Posts Learn Components new Snippets new Categories Tags About Device Beast sponsor
/

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

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

Snippets 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.

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components

Other Code Snippets

More