Laravel version: 6.20.44
I have the following command with an optional date param:
protected $signature = 'do-my-thing {--date?=}';
I look to see if the option has been set:
$dateToDoThing = $this->option('date');
and if it set, I want to use the value:
if ($dateToDoThing) {
// ... validate, create date from string format
$now = Carbon::createFromFormat($dateFormat, $dateToDoThing);
} else {
$now = Carbon::now();
}
So when I run the command, without adding a date, I get the following error:
The "date" option does not exist.
I have instead tried using argument, but now I get:
The "date" argument does not exist.
I thought by adding the ?
after the option in the method signature meant it was optional? I feel like I’m missing something quite obvious here, if anyone can point me in the direction I’d be most grateful.
2
Answers
I managed to resolve the problem with an option, rather than an argument, by doing the following:
In the signature:
protected $signature = 'do-my-thing {--date=}';
In my method:
I didn't need to add the question mark in the signature, because it is an 'option' it is always optional. Hope that helps someone else out there!
Have you tried with optional argument instead of an option ?
This works for me:
Note that when you are expecting an input and not an option you should use
$this->argument();