Throughout this tutorial you will learn how to call an artisan commands from controller in Laravel application. We can execute laravel predefined or custom commands command from controller laravel application.
Call to commands from controller laravel give us a Artisan facade. Laravel Artisan facade that way we can easily run all artisan command with argument. The bellow examples will show you how you can run artisan commands from controller in laravel.
Exmaple 1: Call Console Command
If you have created a console command and want to call it from controller then your need to pass the command signatrure inside artisan call just like below example.
public function index()
{
\Artisan::call('add:records');
}
Exmaple 2: Call Clear chache Command
You can call clear cache command from controller in laravel.
public function index()
{
/* php artisan cache:clear */
\Artisan::call('cache:clear');
dd('cache clear successfully');
}
Exmaple 3: Call DB Seeder
If you have a seeder and want to call it from controller then use below example.
public function index()
{
/* php artisan migrate */
\Artisan::call('db:seed', array('--class' => "UserTableSeeder"));
dd('Seeder run successfully');
}
Example 4: Call Clear Config Command
The below example clear your config easily.
public function index()
{
/* php artisan config:clear */
\Artisan::call(config:clear);
dd('Configuration cache cleared!');
}
I hope these example help you..