How to Execute Artisan Command from Controller in Laravel?
Hi Developer,
In this example, I will show you laravel run artisan command from controller. you can understand a concept of laravel execute shell command. you will learn laravel execute command from controller. I explained simply step by step laravel run artisan from controller example.
Sometimes we require to run php artisan command from route or controller file in out Laravel Application, However 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 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.
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.
Example:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
class ItemController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
Artisan::call("migrate");
dd("done");
}
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Route Pass Multiple Parameters Example
- How to Get All Routes in Laravel?
- How to use Carbon in Laravel Blade or Controller File?
- How to Call Controller Function in Blade Laravel?
- Laravel Redirect to Route from Controller Example
- Laravel Call Function from Same Controller Example
- Laravel - How to Get .env Variable in Blade or Controller?
- How to Call Middleware from Controller in Laravel?
- How to Create Resource Controller in Laravel?
- Laravel Twitter API using Thujohn/twitter Tutorial
- Laravel Create Custom Artisan Command with Example
- Laravel Google reCaptcha using anhskohbo/no-captcha Example
- Laravel Get Route Parameters in Middleware Example
- Laravel Clear Cache from Route, View, Config Example