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

Asked
Active3 hr before
Viewed126 times

8 Answers

laravelcommandconsole
90%

Thanks for contributing an answer to Stack Overflow!, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , Stack Overflow Public questions & answers ,Making statements based on opinion; back them up with references or personal experience.

Try the following:

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

If you need to ask the user for a simple "yes or no" confirmation, you may use the confirm method. By default, this method will return false. However, if the user enters y or yes in response to the prompt, the method will return true.,If necessary, you may specify that the confirmation prompt should return true by default by passing true as the second argument to the confirm method:,Using the onConnection and onQueue methods, you may specify the connection or queue the Artisan command should be dispatched to:,Alternatively, you may pass the entire Artisan command to the call method as a string:

Artisan is the command line interface included with Laravel. Artisan exists at the root of your application as the artisan script and provides a number of helpful commands that can assist you while you build your application. To view a list of all available Artisan commands, you may use the list command:

php artisan list
load more v
72%

Defining Input Expectations,The secret method is similar to ask, but the user's input will not be visible to them as they type in the console. This method is useful when asking for sensitive information such as a password:,Defining Input Expectations Arguments Options Input Arrays Input Descriptions ,Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application. To view a list of all available Artisan commands, you may use the list command:

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application. To view a list of all available Artisan commands, you may use the list command:

php artisan list
load more v
65%

Finally, our command is not registered, therefore we need to add it into our /app/console/Kernel.php file in the $commands array (provide the class path and name):,As expected, this command should do nothing, now you only need to learn how to deal with the command.,This should create a QuizStart class in the /app/console/commands directory with App\Console\Commands namespace.,It's syntax is very simple to understand, basically these are all the ways to use arguments and options in our command (if you want more details, please read the docs):

To create a new console command named quiz:start with class name QuizStart execute the following command in your console :

php artisan make: command QuizStart--command = quiz: start
load more v
75%

Search Answer Titles

protected $signature = 'order:check {--silent=y}'

public
function handle() {
   $silent = $this - > option('silent');
   if ($this - > confirm('This will run the command code continue?') || $silent) {
load more v
40%

Since the {firstName} and {lastName} input requirements were defined as parameters and do not define a default value, the command requires that arguments be supplied for them. Running the following command (without arguments):,Now, when a user is executing the command, an argument for the lastName parameter is not required. The following two command calls would are equivalent:,The example command that has been used so far would produce help information similar to the following:,Similar to array arguments, array options cannot define a default value in the command's signature. A similar effect could be added later in the command's implementation.

For example, the following signature could be used to accept a user's first and last name:

1 < ? php
2
3 // ...
4
5 protected $signature = 'example:command {firstName} {lastName}';
6
load more v
22%

If you're not familiar with Artisan commands, they're command-line functions that you can run to interact with your Laravel application.,You generate a new Artisan command using the following (Artisan!) command:,If you run php artisan from the command line in any Laravel application directory, you'll see a list of all of the Artisan commands available for each app. As you can see, Laravel comes with quite a few enabled out of the box., Creating Artisan commands with the new, simpler syntax in Laravel 5.1 Jun 10, 2015 | laravel, laravel 5.1, artisan

You generate a new Artisan command using the following (Artisan!) command:

$ php artisan make: console SyncJoindInEvents
load more v
60%

Then the base class would validate the data and if it is valid, it would call handle, otherwise display the first validation error on each input:,php artisan password-reset:send notanemail --expires_date="not a date",It would be nice to utilize the framework's validation features to validate artisan command input. I know arguments can be required and an error will be thrown if it is not provided. However thats all about the validation provided before the handle method is called. Same goes for options (with values) there's not much you can do to validate the input unless you manually validate the input in the command yourself., Features →

public
function rules() {
   return [
      'email' => 'email|max:75',
      'expires_date' => 'date|after:today'
   ];
}
load more v

Other "laravel-command" queries related to "Laravel console command - Ask for a non required (optional) input"