Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel how to call Console command from a Database Seeder

Tags:

php

laravel

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

php artisan tntsearch:import App\\MyModel

I want to call this command from my seeder. I've looked at the implementation, and it is complicated enough that I 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 want to 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()?

like image 234
Josh Petitt Avatar asked Mar 14 '17 16:03

Josh Petitt


1 Answers

You can use Artisan facade:

\Artisan::call('tntsearch:import', ['model' => App\User::class]);
like image 135
Alexey Mezenin Avatar answered Oct 07 '22 00:10

Alexey Mezenin