Many people have asked about this error: Target class [DatabaseSeeder] does not exist. It seems to have many root causes, but I cannot determine my root cause. I am using Laravel 6.20.43. The software does not produce any errors when run in the browser.
The error
The error appears when I use this command: php artisan db:seed
Here is DatabaseSeeder.php:
<?php
namespace Database\seeds;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use App\User;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// factory$this->call(UsersTableSeeder::class);
// factory(App\User::class, 10)->create();
// dd('DatabaseSeeder.php TESTING...');
}
}
What have I tried?
- I have tried to add a dd(...) inside DatabaseSeeder::run(). The dd(...) is not executed.
- I have tried
composer update
. The update was performed nicely, but did not resolve the error. - I have tried several use clauses in DatabaseSeeder.php.
- I have tried
php artisan migrate:fresh
- I have tried several combinations of solutions, for example to run migrations before dump-autoload and vice versa.
- I have tried
composer dump-autoload
and this is the output:
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
Generated optimized autoload files containing 4381 classes
- I have tried to redirect the output of
php artisan db:seed
to xdebug so I can analyse step by step what is happening. Good luck is what I needed here, but I ran out of luck. - I have tried to examine the error using
php artisan db:seed -vvv
. Here is the full output:
Illuminate\Contracts\Container\BindingResolutionException : Target class [DatabaseSeeder] does not exist.
at /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:805
801|
802| try {
803| $reflector = new ReflectionClass($concrete);
804| } catch (ReflectionException $e) {
> 805| throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
806| }
807|
808| // If the type is not instantiable, the developer is attempting to resolve
809| // an abstract type such as an Interface or Abstract Class and there is
Exception trace:
1 ReflectionException::("Class DatabaseSeeder does not exist")
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
2 ReflectionClass::__construct()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
3 Illuminate\Container\Container::build()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:681
4 Illuminate\Container\Container::resolve()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:785
5 Illuminate\Foundation\Application::resolve()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:629
6 Illuminate\Container\Container::make()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:770
7 Illuminate\Foundation\Application::make()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:76
8 Illuminate\Database\Console\Seeds\SeedCommand::getSeeder()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:63
9 Illuminate\Database\Console\Seeds\SeedCommand::Illuminate\Database\Console\Seeds\{closure}()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:129
10 Illuminate\Database\Eloquent\Model::unguarded()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:64
11 Illuminate\Database\Console\Seeds\SeedCommand::handle()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
12 Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Util.php:37
13 Illuminate\Container\Util::unwrapIfClosure()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
14 Illuminate\Container\BoundMethod::callBoundMethod()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
15 Illuminate\Container\BoundMethod::call()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:590
16 Illuminate\Container\Container::call()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Command.php:134
17 Illuminate\Console\Command::execute()
/home/billybob/laravel-cursus1/vendor/symfony/console/Command/Command.php:255
18 Symfony\Component\Console\Command\Command::run()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Command.php:121
19 Illuminate\Console\Command::run()
/home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:1009
20 Symfony\Component\Console\Application::doRunCommand()
/home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:273
21 Symfony\Component\Console\Application::doRun()
/home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:149
22 Symfony\Component\Console\Application::run()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Application.php:93
23 Illuminate\Console\Application::run()
/home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:131
24 Illuminate\Foundation\Console\Kernel::handle()
/home/billybob/laravel-cursus1/artisan:37
I truly cannot understand how to debug the output of php artisan db:seed -vvv
. All those files reside in the vendor directory, meaning I cannot examine the program flow easily. Please also explain how I can debug such an error by myself in the future.
CodePudding user response:
It cannot find your DatabaseSeeder
class because your namespace is incorrect.
You need to change the namespace of your DatabaseSeeder
class from:
namespace Database\seeds;
to:
namespace Database\Seeders;
Make sure your composer autoload is setup correctly:
"autoload": {
"psr-4": {
"Database\\Seeders\\": "database/seeders/"
}
}
Alternatively, completely remove all namespaces from your DatabaseSeeder
class and revert the above changes to composer.json
and then run:
composer dump-autoload
then try running:
php artisan migrate:fresh --seed