Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zahidnazirkhan's avatar

Running Seeders via Laravel Controllers

Hello Everybody,

I have a scenario where I need to un specific seeders in order to populate some default data for database tables.

The seeders need to be run via code inside some controller(s).

But when I use

Artisan::call('db:seed');

it throws me errors like

SeederClass Not Found

From documentation, I understand to use composer dumpautoload before actually running new seeders and when I do the same via terminal everything works fine.

How can I fix the issue when running seeders via code inside Laravel Controllers.

Any valuable suggestions are highly obliged. Thank You.

0 likes
8 replies
CorvS's avatar

@zahidnazirkhan You could simply call the run function of your seeder(s) directly.

// For example
(new DatabaseSeeder)->run();

Why exactly do you want to run seeders inside your controllers tho? Are you sure there isn't another way around that? If possible you should seed your database once using the artisan command.

bugsysha's avatar

I agree. Running seeders from controllers is a very bad idea.

bugsysha's avatar

What happens with the requests that come in while dumping autoload is executing?

zahidnazirkhan's avatar

@corvs Thank You for your reply.

Actually, the reason is over time new changes are added to the system and so are new tables (and their default values).

So when a user takes an update, new functionalities in the form of migrations & seeders needs to be run. At that time I am running Artisan::call('db:seed');from within a controller. End users are not supposed to run terminal commands.

For fresh installations, everything works fine via terminal using artisan commands.

CorvS's avatar

@zahidnazirkhan

So when a user takes an update, new functionalities in the form of migrations & seeders needs to be run

Do you have to use migrations/seeders for that? I don't exactly know what those changes look like, but maybe it's better if you wrap them inside an action?

zahidnazirkhan's avatar

@s4muel Sorry for late reply.

Yes, your solution seems to help me out. I have checked it in Dev environment & it worked.

Need to cross verify the same in different environments to see how it behaves.

Thank you.

1 like

Please or to participate in this conversation.