How To Define Artisan Input Expectations In Laravel

admin_img Posted By Bajarangi soft , Posted On 10-09-2020

When writing console commands, it is common to gather input from the user through arguments or options. Laravel makes it very convenient to define the input you expect from the user using the signature property on your commands. The signature property allows you to define the name, arguments, and options for the command in a single, expressive, route-like syntax.

How To Define Artisan Input Expectations In Laravel

1.Arguments

All user supplied arguments and options are wrapped in curly braces.

In the following example, the command defines one required argument: user:

protected $signature = 'email:send {user}';


You may also make arguments optional and define default values for arguments:
 
// Optional argument...
email:send {user?}

// Optional argument with default value...
email:send {user=foo}
 

2.Options

Options, like arguments, are another form of user input. Options are prefixed by two hyphens (--) when they are specified on the command line. There are two types of options: those that receive a value and those that don't. Options that don't receive a value serve as a boolean "switch". Let's take a look at an example of this type of option:
 

protected $signature = 'email:send {user} {--queue}';


In this example, the --queue switch may be specified when calling the Artisan command. If the --queue switch is passed, the value of the option will be true. Otherwise, the value will be false:
 

php artisan email:send 1 --queue
 

Options With Values
Next, let's take a look at an option that expects a value. If the user must specify a value for an option, suffix the option name with a = sign:

protected $signature = 'email:send {user} {--queue=}';


In this example, the user may pass a value for the option like so:

php artisan email:send 1 --queue=default


You may assign default values to options by specifying the default value after the option name. If no option value is passed by the user, the default value will be used:

email:send {user} {--queue=default}
 

Option Shortcuts

To assign a shortcut when defining an option, you may specify it before the option name and use a | delimiter to separate the shortcut from the full option name:
 

email:send {user} {--Q|queue}
 

3.Input Arrays

If you would like to define arguments or options to expect array inputs, you may use the * character. First, let's take a look at an example that specifies an array argument:

email:send {user*}


When calling this method, the user arguments may be passed in order to the command line. For example, the following command will set the value of user to ['foo', 'bar']:

php artisan email:send foo bar


When defining an option that expects an array input, each option value passed to the command should be prefixed with the option name:

email:send {user} {--id=*}

php artisan email:send --id=1 --id=2
 

4.Input Descriptions

You may assign descriptions to input arguments and options by separating the parameter from the description using a colon. If you need a little extra room to define your command, feel free to spread the definition across multiple lines:

protected $signature = 'email:send
{user : The ID of the user}
{--queue= : Whether the job should be queued}';

Related Post