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 Databaseseeds; use IlluminateDatabaseSeeder; use IlluminateSupportFacadesDB; use IlluminateDatabaseEloquentModel; use AppUser; class DatabaseSeeder extends Seeder { /** * Seed the application's database. * * @return void */ public function run() { // factory$this->call(UsersTableSeeder::class); // factory(AppUser::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 > IlluminateFoundationComposerScripts::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:
IlluminateContractsContainerBindingResolutionException : 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 IlluminateContainerContainer::build() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:681 4 IlluminateContainerContainer::resolve() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:785 5 IlluminateFoundationApplication::resolve() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:629 6 IlluminateContainerContainer::make() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:770 7 IlluminateFoundationApplication::make() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:76 8 IlluminateDatabaseConsoleSeedsSeedCommand::getSeeder() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:63 9 IlluminateDatabaseConsoleSeedsSeedCommand::IlluminateDatabaseConsoleSeeds{closure}() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:129 10 IlluminateDatabaseEloquentModel::unguarded() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php:64 11 IlluminateDatabaseConsoleSeedsSeedCommand::handle() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36 12 IlluminateContainerBoundMethod::IlluminateContainer{closure}() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Util.php:37 13 IlluminateContainerUtil::unwrapIfClosure() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93 14 IlluminateContainerBoundMethod::callBoundMethod() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37 15 IlluminateContainerBoundMethod::call() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Container/Container.php:590 16 IlluminateContainerContainer::call() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Command.php:134 17 IlluminateConsoleCommand::execute() /home/billybob/laravel-cursus1/vendor/symfony/console/Command/Command.php:255 18 SymfonyComponentConsoleCommandCommand::run() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Command.php:121 19 IlluminateConsoleCommand::run() /home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:1009 20 SymfonyComponentConsoleApplication::doRunCommand() /home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:273 21 SymfonyComponentConsoleApplication::doRun() /home/billybob/laravel-cursus1/vendor/symfony/console/Application.php:149 22 SymfonyComponentConsoleApplication::run() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Console/Application.php:93 23 IlluminateConsoleApplication::run() /home/billybob/laravel-cursus1/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:131 24 IlluminateFoundationConsoleKernel::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.
Advertisement
Answer
It cannot find your DatabaseSeeder
class because your namespace is incorrect.
You need to change the namespace of your DatabaseSeeder
class from:
namespace Databaseseeds;
to:
namespace DatabaseSeeders;
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