Home / Snippets / How to Call Console Command From Anywhere in Laravel Codebase
How to Call Console Command From Anywhere in Laravel Codebase cover

How to Call Console Command From Anywhere in Laravel Codebase

218

3 years ago

0 comments

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'
]);
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this