Laravel call artisan command from route or controller

Laravel call artisan command from route or controller

  • 14 July, 2018
  • Renish Khunt
  • Laravel
  • 153

Laravel provides awesome artisan command functionality. using artisan command we can create the controller and model file or clear the blade file cache and many more. How we can call artisan command from controller or route.

Let’s see how we can call artisan command from route or controller. So we can easily call artisan command directly from URL. Laravel provides functionality for programmatically execute commands using Artisan::call¬†function.

So let’s see, How we can execute command programmatically. First of all, we are run the migration command using programmatically.

Route::get('command/migrate', function () {
    $exitCode = \Artisan::call('migrate');
    dd("Done");
});

Now, we run migration command using “command/migrate” URL. it’s too easy to run migration¬†command programmatically.

Now, Let’s see the example for clear blade cache programmatically

Route::get('command/clear/cache', function () {
    $exitCode = \Artisan::call('view:clear');
    dd("Done");
});

It’s too easy to execute the Laravel artisan command using programmatically route or controller.

If you face any issue write the comment below.