Send variable to Command from Controller Laravel

3

Today I call command through controler perfectly, but I'd like to send variables too. In controller , I call command like this:

\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 as follows:

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

So, the name of command would look like this:

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

The controller does not show error, but when I try to get this variable in handle() it 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 anyone help me?

    
asked by anonymous 27.12.2016 / 15:38

1 answer

3

I was able to find out

The call to the command can be done 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 the 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],
    ];
}

Inside the handle () I searched for arguments using the following method:

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

This way I was able to retrieve the submitted arguments.

    
27.12.2016 / 17:44