Home » Php » Run Laravel 5 seeder programmatically instead of from CLI

Run Laravel 5 seeder programmatically instead of from CLI

Posted by: admin November 30, 2017 Leave a comment

Questions:

Is there a way to run the Laravel 5 seeder from within PHP rather than from the command line. The hosting I am using doesn’t allow me to use the command line. Just to confirm I want to do the equivalent of this but in my app code:

php artisan db:seed
Answers:

You can use the following method:

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

To get the output of the last run command, you can use:

Artisan::output();

Questions:
Answers:

You can also call directly the Seeder class if needed.
Just make sure you did a composer dump-autoload if you created your seeder manually.

From there code is very straightforward :

$seeder = new YourSeederClass();
$seeder->run();