8

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()?

1 Answer 1

17

You can use Artisan facade:

\Artisan::call('tntsearch:import', ['model' => App\User::class]);
Sign up to request clarification or add additional context in comments.

2 Comments

Anyway to make this command produce output? Some import/seeding can take some time. Thank you.
If the called artisan command asks for (yes/no) answers in several questions, how do I specify them? Otherwise, the seeder simply never ends, since there's no output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.