Laravel – Call database seeder class from controller

Some times you may need to see the results of specific seeder class in a web page instead of terminal.

Thus instead of calling the seeder class via terminal:

Call the class from within a controller, and here is the way I used to achieve that:

First, inside your seeder class add the namespace you want, I do that by writing the folders as namespace.

UserSeeder

UserController

Very Important to know that this affect the seeder class, now if you want to use DB Facade earlier you got it by default, but now you must add it to your seeder manually/strong>, the same for Log Facade and other Facades…

Errors

If you are going to use the same seeder in terminal after doing the above changes you will get this error:
ReflectionException : Class StreetSeeder does not exist
And this happens because now Laravel does not recognize the seeder class because of the new namespace you added.

To Fix the error

Thanks for reading

mhmd

Hello, I'm Mohammad Daka, software engineer who love coding. In my spare time I blogs about JavaScript,PHP, Chrome Extensions, helpful resources and tools, I'm also working on my own projects. Don't hesitate to contact me at my twitter account: @mohammad_daka :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *