James Fannon
James Fannon

Reputation: 271

Laravel console command - Ask for a non required (optional) input

I'm trying to create a optional console command.

$phone = $this->ask('Enter a phone number for the Seller (blank if not supplied)');

The problem is that if left blank I'll get:

[ERROR] A value is required.

Is there a work around for this to not require a response? Maybe something like ->nullable() or similar?

Upvotes: 14

Views: 5329

Answers (1)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

By default answer to console question is required. Empty string is considered an empty answer, hence the error. You need to provide a default value and that should do the trick.

Try the following:

$phone = $this->ask('Enter a phone number for the Seller (blank if not supplied)', false);

If no phone number has been provided it will be given FALSE value. You can see if number was provided with

if ($phone !== FALSE) { //notice strict comparison !==
  // number has been provided
} else {
  // no number provided
}

Upvotes: 22

Related Questions