Php Artisan and Tinker not finding laravel project classes - php

I happened to open two instances of Laragon at the same time. After that php artisan migrate started throwing strange errores like "Interface not found" when that interface is actually there and was correctly imported. Tried different migrations and also throws random errors. I even tried with an old (already migrated) migration, copy pasted the code and it also didn't work. Always related to classes/interfaces not being found.
I've just noted that a simple test:
<?php
use App\Book;
use Illuminate\Database\Migrations\Migration;
class BookTest extends Migration
{
public function up()
{
echo Book::HARDCOVER;
}
}
doesn't work; it says: 'Class 'App\Book' not found' . The book class is there and was imported from phpstorm with a simple click. So, php artisan isn't finding any of my project classes.
I've just confirmed that tinker can't find the classes either.
Ok, I've just noticed that if I change in the book class the namespace to '\App\Models\Store' (where the file actually is) and I do from tinker something like \App\Models\Store\Book::HARDCOVER, then it actually works. The thing I don't get is why it's now (suddenly) needing me to update the name space to work...

Don't know how but my composer version was 2 so I had to downgrade to 1.10 and all works again.

Related

Adjust Seeders integration for Package Development in Laravel 8

I developed a package for an api in laravel.7.x. That api is delivered as an package for laravel. Now I want to upgrade it to laravel 8. Unfortunately I cannot get the seeders to work.
all package Seeders should executed after the
php artisan migrate:fresh --seed
command.
Appearantly the Seeder classes are not found. for Example Target class [PostSeeder] does not exist.
But it does exist, it's even in the same namespace.
Now I'm trying to start in with a DatabaseSeeder and this one IS found. But from there the other Seeders can't be triggered.
Does anyone has an Idea what can be tried or has a hint or code snippet for this problem?
Many thanks in advance.
alright, the issue is solved.
Use the new way of including factories in laravel 8 i.e Models have this new function:
use Acme\YourPackage\Database\Factories\PostFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
protected static function newFactory()
{
return PostFactory::new();
}
and make sure to call it like:
enter $this->call('Acme\YourPackage\Database\Seeders\PostSeeder');
after that make your sure your namespace and class paths are set correctly.

Laravel 5.1 View not found

this seems to be an issue that pops up every now and then within Laravel. I was writing a CRUD controller with a view to go with it, however upon testing I got the InvalidArgumentException in FileViewFinder.php line 137: View [bookingcrud.index] not found error. Here is my code:
routes.php:
Route::resource('bookingcrud', 'BookingsCrudController');
BookingsCrudController.php
use uasc\Http\Requests;
use uasc\Http\Requests\CrudCreateRequest;
use uasc\Http\Requests\CrudUpdateRequest;
use uasc\Http\Controllers\Controller;
use Auth;
use DB;
use Illuminate\Pagination\Paginator;
use Illuminate\Http\Request;
class BookingsCrudController extends Controller {
public function index()
{
if (!Auth::check() || Auth::user()->authority < 1) {
return redirect('/login');
}
$raw = "select * from bookings";
$bookings = DB::select($raw);
$paginatedBookings = new Paginator($bookings, 1);
return view('bookingcrud.index')->with('bookings', $paginatedBookings);
}
}
And a view located in ~/laravel/resources/views/bookingcrud/index.blade.php
No matter what is in this view file whether its markup from a working view or just the word "cheese" i will always get:
InvalidArgumentException in FileViewFinder.php line 140:
View [bookingcrud.index] not found.
I tested the same view in a known working controller and got the same error however I tested a known working view on the same CRUD controller and it worked. I have also tried changing the directory of the view and renaming it but i'll get the same error with the "View [bookingcrud.index]" changing appropriately. I made sure the permissions of the file and directories were full for testing.
Since first getting the error I have upgraded to 5.1.1 from 5.0.26 (which is the version the error originated on for me) and ran composer update. Also from looking at threads with the same error I have also ran artisan config:clear
I am developing on Windows 8.1 with Homestead 2.0.17 deployed with Virtual Box.
Any help would much appriciated at this point it is doing my head in.
Please use terminal
homestead ssh > Press Enter
vagrant#homestead cd /Path_of_Your_Project/
vagrant#homestead:~/Path_of_Your_project php artisan cache:clear
vagrant#homestead:~/Path_of_Your_project php artisan config:cache
Sorry about my English
Turns out I had spelt blade incorrectly, took a second pair of eyes to notice it though.
$ ls resources/views/crud/booking/
crud.balde.php index.balde.php
Was definitely a lesson to always double check the small things when debugging.
Thanks for the help.
For someone who don't have SSH Access, There are two ways to solve this problem.
If you don't have any plugin of default Laravel package,
First, Just remove the bootstrap/cache/config.php to solved the file,
OR
If you have any plugin of default Laravel package,
Change all related path mentioned bootstrap/cache/config.php into the exact path which allocated the laravel project.
Remember that Linux is case sensitive!
I had something like this:
return view('MyView', $data);
And it was working on my environment (Mac OS), but not on the deployment server (Ubuntu)!

Laravel 5: "class App\Commands\Command not found"

In Laravel 5, I just ran php artisan make:command doMagicStuff to create a generic command with a die() statement in the handler. Whenever I try to `$this->dispatch(new DoMagicStuff()), however, I get:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'App\Commands\Command' not found
If I remove extends Command it works, though obviously not correctly.
Is there some reason it can't find App\Commands\Command ? (Note: In ./app/Commands, there is no Command.php, so that is a hint, though it might live under the vendor directory. I did a grep for "class Command", but no luck.
Did you clone the repo from github? The repository there has a Command.php with nothing in it, just empty abstract. My projects also have it, so... weird.
Here: https://github.com/laravel/laravel/tree/5.0/app/Commands
Whenever you create a command, it is namespaced as namespace App\Commands; and you extends Command, so it will look in the App\Commands dir for a Command class, since it exists in the repo, it should exist in your copy too.

Laravel Artisan Migrate [BadMethodCallException]

When I try to run php artisan migrate to migrate a missing migration to my database. I get the following exception:
[BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::up()
See down to get the complete log and stack trace.
I run the command on the console local on my own computer. But on my server it does not work either.
I have tried already the following:
1. composer update
2. artisan dump-autoload
3. Delete /vendor and do composer install
This (https://github.com/cartalyst/sentry/issues/257) has not helped because i don't have a compile.php file. (Cause on local development this is disabled by default from laravel.)
If you need more informations like the complete migration code. Please ask.
This is the complete stack trace from the log http://snippi.com/s/lz5z86f (I have put it into a snippet cause it is quite long.)
I had another class which had the same filename like the migration.
Cause of this the exception was thrown. Renaming and executeing artisan dump-autoload helped.
I had the same problem and then I realized that my migrations filename differed from the class name and that fixed the problem for me. Try that one.
Could you show us your migration since the Exception tells you that the method used for creating the tables etc. is not there. In every migration the layout should look something like this:
public function up() {
Schema::create('users', function($table)
{
$table->increments('id');
});
}
public function down() {
Schema::drop('users');
}
Maybe you are calling a Class instead of the ClassSeeder in your DatabaseSeeder or Seeder

Cannot use X as Y because the name is already in use, even though it's not

I'm using PHP 5.4, and have a PSR-0 class structure similar to the following.
A\Library\Session.php:
namespace A\Library;
class Session { ... }
My\Application\Session.php:
namespace My\Application;
class Session { ... }
My\Application\Facebook.php:
namespace My\Application;
use A\Library\Session;
class Facebook { ... }
When I try to run the application, I get the following error:
Cannot use A\Library\Session as Session because the name is already in use in My\Application\Facebook.php
Even though it's not, at least not in this file. The Facebook.php file declares only the Facebook class, and imports exactly one Session class, the A\Library one.
The only problem I can see is that another Session class exists in the same namespace as the Facebook class, but as it was never imported in the Facebook.php file, I thought it did not matter at all.
Am I wrong (in that case please point to the relevant documentation), or is this a bug?
There is a bug confirmed in PHP that may affect the behavior you see. It is supposed to fatal error, but with opcache enabled, it may still execute flawlessly.
https://bugs.php.net/bug.php?id=66773
If it still concerns you, please vote for the bug.
No, this is not a bug. As mentioned in Using namespaces: Aliasing/Importing
use A\Library\Session;
is the same as:
use A\Library\Session as Session;
So try using something like:
use A\Library\Session as AnotherSessionClassName;
The only problem I can see is that another Session class exists in the
same namespace as the Facebook class, but as it was never imported in
the Facebook.php file, I thought it did not matter at all.
Yes, it does matter. This is why you don't need to "import" classes from the same namespace. If you have conflicting names from different namespaces, you need to alias the class.
namespace My\Application;
use A\Library\Session as ASession; // choose a proper alias name here
class Facebook { ... }
I've read the thread about the issue, but I tested on many PHP versions (php 5.5, 5.6, 7.*, x32, x64, vc11, vc14, vc5). I'm using Laravel with Laragon. But, when I build up the server with php artisan serve (and open the server at http://localhost:8000) I have the problem of "the namespace that some Class was already used" and stuff.
I tested with and without opcache extension and nothing works, then I tested the virtual domain that Laragon provides and... voila, the error just disappeared and now I can work OK. I don't know what was happening, my namespaces were OK, I had an alias but the same code works in many machine without problem (AWS, local, prod, dev, etc) but only in my machine I had the problem just as I described it.
So, if someone is working with Laravel (5.1) and is having this issue, try the virtual host of Laragon.

Categories