Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 585
James Fannon Avatar asked Jan 13 '16 17:01

James Fannon


1 Answers

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
}
like image 105
jedrzej.kurylo Avatar answered Oct 07 '22 16:10

jedrzej.kurylo