Call Artisan Command from Controller in Laravel 8

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Call Artisan Command from Controller in Laravel 8

Now today in this tutorial,i will show here you how we can execute artisan commands from controller in Laravel 8? Here we are call artisan command from controller in laravel 8 application. It will run artisan command from controller in laravel 8 application.

Now we can easy execute artisan command from controller in laravel 8. So we can do it by using Artisan facade. In Laravel Artisan facade that way we can easily run the all artisan command also with argument.

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

So Here in bellow example we can learn how can we run artisan commands from controller.

Exmaple 1

Now here bellow example we 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

Now 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!');
}

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →