Search code examples
phplaravellaravel-5.4laravel-artisan

Laravel artisan programmatically running command with any parameter without key


In my application , i would like to simulate php artisan command outside CLI, in which i am doing it inside controller.

I am able to get it running with:

$execute = Artisan::call($commandPart[0], [
        'companyId' => $commandPart[1]
]);

However, it is working if i am specifying it's key for the parameter. Meanwhile, what i am trying to achieve is to pass in list of command without keyed parameter, as i want it to be flexible.

I tried,

$execute = Artisan::call($commandPart[0], [
    $commandPart[1]
]);

and

$command = 'organization:tree_readjust 1';
$execute = Artisan::call($command);

However this both scenario will produce error. Any solution for this?

Thank you before.


Solution

  • according to documentation The call method accepts the name of the command as the first argument, and an array of command parameters as the second argument. so, first make sure $commandPart[0] is valid command and you can pass array arguments on second param. ex:

    $command = 'company:delete';
    $companyId = 1;
    $commandPart = [$command, $companyId];
    $execute = Artisan::call($commandPart[0], [
            'companyId' => $commandPart[1]
    ]);
    

    Edit:

    You cannot call arguments without key. for flexibility I think You can create some logic to make array params and pass it on artisan call $execute = Artisan::call($command, $params);