Reputation: 1666
I have a command line command for creating a new company. In the command it uses ask() to interrogate the user for company billing address information. One of the fields, 'address2', is optional and it used to work just fine in Laravel 5.0 when the user would just hit return to leave it blank. I have read up on the new 5.1 commands. I have refactored this command to Laravel 5.1 specs but I cannot figure out how to use the ask() function to allow a field to be blank. How do I ask() the user for an optional address field? I've gone as far as providing a default value, eg:
$address = $this->ask("address2", "n/a");
But I can't have 'n/a' or anything for that matter in an unnecessary address2 field. I want it to be blank, but, doing this:
$address = $this->ask("address2", "");
or this:
$address = $this->ask("address2", null);
results in this error: "a value is required."
I'm stuck. How do I allow for blank optional fields in Laravel 5.1?
Upvotes: 1
Views: 457
Reputation: 92785
You can do something along the lines of
$address2 = $this->askOptional('address2');
private function askOptional($question)
{
$answer = $this->ask($question, '(empty)');
return '(empty)' == $answer ? null : $answer;
}
Upvotes: 1