Home » Php » php – Any imaginary parameter in the laravel command

php – Any imaginary parameter in the laravel command

Posted by: admin February 25, 2020 Leave a comment

Questions:

Is it possible tu pass any imaginary parameter with laravel command? This parameter shouldn’t be defined in command implementation.
For example here is command signature:

protected $signature = 'generateInvoices {--filter=*}';

Then I can use this command by:

php artisan generateInvoices --filter=filter1 --filter=filter2

but I want to use other params:

php artisan generateInvoices --filter=filter1 --filter=filter2 --somethingelse=23 --thisIsNextParam=4 myImaginaryparam=54
How to&Answers:

You can not do dynamic option “parameters” (marked with --), but you can do an array of optional arguments.

protected $signature = 'generateInvoices {--filter=*} {extraArgs?*}';

Then running the extra parts without the --, you can get an array of extra arguments that you need to parse separately.

php artisan generateInvoices --filter=filter1 --filter=filter2 somethingelse=23 thisIsNextParam=4 myImaginaryparam=54

Using $this->argument('extraArgs)` will give you an array of parsable values.

Results of all data being available to your command:

"Options: "
array:9 [
  "filter" => array:2 [
    0 => "filter1"
    1 => "filter2"
  ]
  "help" => false
  "quiet" => false
  "verbose" => false
  "version" => false
  "ansi" => false
  "no-ansi" => false
  "no-interaction" => false
  "env" => null
]
"Arguments: "
array:2 [
  "command" => "generateInvoices"
  "extraArgs" => array:3 [
    0 => "somethingelse=23"
    1 => "thisIsNextParam=4"
    2 => "myImaginaryparam=54"
  ]
]