ReflectionException in Laravel 5 When Seeding Database (Class Does not Exist)

less than a minute of reading

It took me a while to realize what is going on with this strange issue. You did a migration that worked fine? Then you wanted to populate newly created table by seeding your database? You made a proper file, you called the file inside DatabaseSeeder.php using exactly the same class name as in the recent seeding file? You are absolutely sure that everything is named as it should be and all files exist in a proper place?

Surprisingly the solution to this issue is quicker than the introduction you have just read.

Context

So probably after php artisan migrate you run php artisan db:seed and you get ReflectionException with irrational message saying that the mentioned class does not exist?

Solution

After migration and just before seeding, all you have to do is run one additional command. Simply dump autoloads with composer dump-autoload.

Accoding to the documentation:

If you need to update the autoloader because of new classes in a classmap package for example, you can use dump-autoload to do that without having to go through an install or update.

Finally run the seed command again: php artisan db:seed.

Now everything should work as expected. No exceptions and fully populated database.

I hope that you have saved some time with this article.


Words: 214
Published in: Laravel · PHP
Last Revision: October 25, 2022

Related Articles   📚