Run artisan command in laravel 5
Asked Answered
M

6

58

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

Miocene answered 15/5, 2016 at 8:45 Comment(0)
A
85

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

Alvarez answered 15/5, 2016 at 8:51 Comment(3)
@Alexey Mezenin, It seems you can help me. This about infyom laravel generator. Look here : #40953401Chiliasm
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.Rambutan
@Alexey Mezenin can you see this #51858947Miocene
C
24

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
Cordy answered 24/10, 2019 at 13:19 Comment(0)
I
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/

Intransitive answered 6/6, 2021 at 18:29 Comment(0)
L
2

Remove php artisan part and try:

Route::get('/run', function () {
    Artisan::call("migrate");
});
Lugo answered 8/2, 2022 at 8:50 Comment(0)
H
0

Command Job,

Path : {project-path}/app/Console/Commands/RangeDatePaymentsConsoleCommand.php

This is the Job that runs with the artisan command.

class RangeDatePaymentsConsoleCommand extends Command {
    protected $signature = 'batch:abc {startDate} {endDate}';
    ...
}

web.php,

Path : {project-path}/routes/web.php

web.php manage all the requests and route to relevant Controller and can have multiple routes for multiple controllers and multiple functions within the same controller.

$router->group(['prefix' => 'command'], function () use ($router) {
    Route::get('abc/start/{startDate}/end/{endDate}', 'CommandController@abc');
});

CommandController.php,

Path : {project-path}/app/Http/Controllers/CommandController.php

This Controller is created for handle artisan commands and name can be vary but should be same to web.php Controller name and the function name.

class CommandController extends Controller {

    public function abc(string $startDate, string $endDate) {
        $startDate = urldecode($startDate);
        $endDate = urldecode($endDate);

        $exitCode = Artisan::call('batch:abc',
            [
                'startDate' => $startDate,
                'endDate' => $endDate
            ]
        );
        return 'Command Completed Successfully. ';
    }

Request : http://127.0.0.1:8000/command/abc/start/2020-01-01 00:00:00/end/2020-06-30 23:59:59

It's can be access through the web browser or Postman after startup the server. Run this command to start the php server at {project-path}

php -S 127.0.0.1:8080 public/index.php
Helianthus answered 20/7, 2020 at 10:8 Comment(1)
Please add some explanation to your answer such that others can learn from itForerunner
I
0

Method #1: Using route

Route::get('run-it', function () {
    (new \App\Console\Commands\ThisIsMyCommand())->handle();
});

Method #2: Using command line

php artisan command:this_is_my_command
Idun answered 8/2, 2023 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.