Tuesday, March 14, 2017

Laravel how to call Console command from a Database Seeder

I am writing some seeders for my laravel database. I'm using Laravel Scout TNTSearch driver.

This has a console command

php artisan tntsearch:import App\\MyModel

I would like to call this command from my seeder. I've looked at the implementation, and it is complicated enough that I really don't want to refactor, wait for a merged pull request, or copy-and-paste the implementation into my files.

Inside the seeder I tried:

$this->call('tntsearch:import', ['model' => App\User::class]);

but $this refers to the seeder, and the call method is expecting another seeder, not a console command.

I would really like to just call the artisan command from inside my seeder. Is this possible?

UPDATE:

I ended up doing this inside the seeder:

exec('php artisan tntsearch:import App\\User');

This works, but feels dirty. Is there another easy way without using exec()?



via Josh Petitt

Advertisement