Skip to content
Advertisement

Cannot resolve this error: “Target class [DatabaseSeeder] does not exist.”

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:

JavaScript

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:
JavaScript
  • 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:
JavaScript

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:

JavaScript

to:

JavaScript

Make sure your composer autoload is setup correctly:

JavaScript

Alternatively, completely remove all namespaces from your DatabaseSeeder class and revert the above changes to composer.json and then run:

JavaScript

then try running:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement