Laravel Console Command - Ask For A Non Required (optional) Input

- 1 answer

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?



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