Posts Learn Snippets Categories Tags About
/

How to Call Console Command From Anywhere in Laravel Codebase

Learn how to execute or call your console command from anywhere in your Laravel codebase.

Created on Jul 04, 2021

7 views

To call any of the artisan console commands from anywhere in the Laravel codebase, you can make use of the "Illuminate\Support\Facades\Artisan" facade.

You can use the "call" method provided by this facade and then specify the command as the first argument and any parameter for the command on the second parameter.

use Illuminate\Support\Facades\Artisan;

Artisan::call('migrate'); // equivalent of calling "php artisan migrate"
Artisan::call('make:model User'); // equivalent of calling "php artisan make:model User"

You can programmatically call the artisan console command from anywhere within your application just that ensure the application runs through the code to get the command executed.

Console Command With Parameters
To pass in the parameter for the command, you can add it as key and value pair for the second parameter of the "call" method like below.
Artisan::call('mail:send', [
    'user' => User::first(), '--queue' => 'default'
]);

Created by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.