How to run an artisan command from a controller

October 26, 2018

Apart from within another command, I am not really sure I can think of a good reason to do this. But if you really want to call a Laravel command from a controller (or model, etc.) then you can use Artisan::call()

<?php
Artisan::call('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);

One interesting feature that I wasn't aware of until I just Googled this to get the right syntax is Artisan::queue(), which will process the command in the background (by your queue workers):

<?php
Route::get('/foo', function () {
    Artisan::queue('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);
    //
});

If you are calling a command from within another command you don't have to use the Artisan::call method - you can just do something like this:

<?php
public function handle()
{
    $this->call('email:send', [
        'user' => 1, '--queue' => 'default'
    ]);
    //
}