Laravel Run Artisan Command From Controller Example

Hi Guys,

Today,I will describe you how to you can execute artisan commands from controller in laravel. we will explain laravel run artisan command from controller.we are call artisan command from controller in laravel. it will run artisan command from controller laravel.

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

Artisan facade have two method call() and queue(), queue() through we can simply make process in queue like email sending etc. So let's see simple example with route. In this example we run "php artisan migrate" command using artisan facade. Here i bellow example you can learn how you can run artisan commands from route.

Exmaple:1

Here In this example, 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:2

In this 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:3

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

In this 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...

Post a Comment

0 Comments