Run artisan command in laravel 5
Solution 1
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
Solution 2
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
Solution 3
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'
]);
//
}
Comments
-
paranoid almost 2 years
I have controller like this
public function store(Request $request) { Artisan::call("php artisan infyom:scaffold {$request['name']} --fieldsFile=public/Product.json"); }
Show me error
There are no commands defined in the "php artisan infyom" namespace.
When I run this command in CMD it work correctly
-
paranoid almost 8 yearsThanks alot. I forget remove it.
-
samuel toh over 7 years@Alexey Mezenin, It seems you can help me. This about infyom laravel generator. Look here : stackoverflow.com/questions/40952901/…
-
Olotin Temitope about 7 years
public function checkBooking(Request $request) { Artisan::call('booking:check', []); }
The first argument is your command signature. if you are not passing external data you can pass in an empty array. -
paranoid over 5 years@Alexey Mezenin can you see this stackoverflow.com/questions/51858447/…
-
Nico Haase almost 4 yearsPlease add some explanation to your answer such that others can learn from it