Call Artisan Command from Controller in Laravel 7/6

10-Apr-2023

.

Admin

Hi guys,

In this tutorial,i will tell you how you can execute artisan commands from controller in Laravel 7/6? we are call artisan command from controller in laravel 7/6. it will run artisan command from controller laravel 7/6.

you can easy execute artisan command from controller in laravel 6. we can do it using Artisan facade. Laravel Artisan facade that way we can easily run all artisan command with argument.

Artisan facade have two method call() and queue() through we can simply make process in call like seeder and migration run etc.

Here i bellow example you can learn how you can run artisan commands from controller.

Exmaple 1


Here bellow example you can call artisan cache clear command from controller

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

/* php artisan cache:clear */

\Artisan::call('cache:clear');

dd('cache clear successfully');

}

Exmaple 2

Here bellow example you can call artisan migrate command from controller

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

/* php artisan migrate */

\Artisan::call('migrate');

dd('all migration run successfully');

}

Exmaple 3

Here bellow example you can call artisan run seeder command from controller

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

/* php artisan migrate */

\Artisan::call('db:seed', array('--class' => "AdminSeeder"));

dd('Seeder run successfully');

}

Exmaple 4

Here bellow example you can call artisan run configration clear command from controller

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

/* php artisan config:clear */

\Artisan::call(config:clear);

dd('Configuration cache cleared!');

}

It will help you....

#Laravel 7

#Laravel

#Laravel 6