Send Command variable from Laravel Controller

Asked

Viewed 144 times

3

Today I call a command for controler perfectly, but I would like to send variables as well. In controller, call the command that way:

\Artisan::call('syncustomer:sav');

The name attribute of the current command:

protected $name = 'syncustomer:sav';

In the documentation I saw that I could pass the variables in the following way:

\Artisan::call('syncustomer:sav', ['[email protected]']);

Thus the name of command would look like this:

protected $name = 'syncustomer:sav {data}';

The controller does not show error, but when I try to catch this variable in handle() gives error saying that there is no argument data:

public function handle(){
    $email = $this->argument('data');
    DB::table('customer')
       ->where('email', '[email protected]')
       ->update(array('email' => $email));
}

The data argument does not exist.

Can someone help me?

  • 1

    Already tried using associtative array? ['data' => '[email protected]']

  • Yes, it gives the same problem

  • That’s strange, because in doc he’s doing like this: https://laravel.com/docs/5.3/artisan#programmatically-executing-Commands

  • 1

    I figured out how to do it, I’ll put it in answer

1 answer

3

I was able to find out

the call to command can be made by sending the variables (arguments) as an associative array, as suggested by Juniornunes

\Artisan::call('syncustomer:sav', array('data' => $data, 'customer' => $customer));

Now, to create these arguments in command, I need to create two methods for mapping

protected function getArguments()
{
    return [
        ['data', InputArgument::REQUIRED,
            'An example argument.'],
        ['customer', InputArgument::REQUIRED,
            'An example argument.']
    ];
}

protected function getOptions()
{
    return [
        ['data', null, InputOption::VALUE_REQUIRED,
            'An example option.', null],
        ['customer', null, InputOption::VALUE_REQUIRED,
            'An example option.', null],
    ];
}

Within Handle() I searched the arguments with the following method:

$data = $this->argument('data');
$customer = $this->argument('customer');

This way I was able to recover the arguments sent.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.