Run artisan command in laravel 5
Apart from within another command, I am not really sure I can think of a good reason to do this. But if you really want to call a Laravel command from a controller (or model, etc.) then you can use Artisan::call()
Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
One interesting feature that I wasn't aware of until I just Googled this to get the right syntax is Artisan::queue()
, which will process the command in the background (by your queue workers):
Route::get('/foo', function () {
Artisan::queue('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});
If you are calling a command from within another command you don't have to use the Artisan::call
method - you can just do something like this:
public function handle()
{
$this->call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
}
Source: https://webdevetc.com/programming-tricks/laravel/general-laravel/how-to-run-an-artisan-command-from-a-controller/
If you have simple job to do you can do it from route file. For example you want to clear cache. In terminal it would be php artisan cache:clear In route file that would be:
Route::get('clear_cache', function () {
\Artisan::call('cache:clear');
dd("Cache is cleared");
});
To run this command from browser just go to your's project route and to clear_cache. Example:
http://project_route/clear_cache
You need to remove php artisan
part and put parameters into an array to make it work:
public function store(Request $request)
{
Artisan::call("infyom:scaffold", ['name' => $request['name'], '--fieldsFile' => 'public/Product.json']);
}
https://laravel.com/docs/5.2/artisan#calling-commands-via-code