Artisan Console
Introduction
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
Every command also includes a "help" screen which displays and describes the command's available arguments and options. To view a help screen, precede the name of the command with help
:
php artisan help migrate
Tinker (REPL)
All Laravel applications include Tinker, a REPL powered by the PsySH package. Tinker allows you to interact with your entire Laravel application on the command line, including the Eloquent ORM, jobs, events, and more. To enter the Tinker environment, run the tinker
Artisan command:
php artisan tinker
You can publish Tinker's configuration file using the vendor:publish
command:
php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"
Command Whitelist
Tinker utilizes a white-list to determine which Artisan commands are allowed to be run within its shell. By default, you may run the clear-compiled
, down
, env
, inspire
, migrate
, optimize
, and up
commands. If you would like to white-list more commands you may add them to the commands
array in your tinker.php
configuration file:
'commands' => [
// App\Console\Commands\ExampleCommand::class,
],
Alias Blacklist
Typically, Tinker automatically aliases classes as you require them in Tinker. However, you may wish to never alias some classes. You may accomplish this by listing the classes in the dont_alias
array of your tinker.php
configuration file:
'dont_alias' => [
App\User::class,
],
Writing Commands
In addition to the commands provided with Artisan, you may also build your own custom commands. Commands are typically stored in the app/Console/Commands
directory; however, you are free to choose your own storage location as long as your commands can be loaded by Composer.
Generating Commands
To create a new command, use the make:command
Artisan command. This command will create a new command class in the app/Console/Commands
directory. Don't worry if this directory does not exist in your application, since it will be created the first time you run the make:command
Artisan command. The generated command will include the default set of properties and methods that are present on all commands:
php artisan make:command SendEmails
Command Structure
After generating your command, you should fill in the signature
and description
properties of the class, which will be used when displaying your command on the list
screen. The handle
method will be called when your command is executed. You may place your command logic in this method.
For greater code reuse, it is good practice to keep your console commands light and let them defer to application services to accomplish their tasks. In the example below, note that we inject a service class to do the "heavy lifting" of sending the e-mails.
Let's take a look at an example command. Note that we are able to inject any dependencies we need into the command's handle
method. The Laravel service container will automatically inject all dependencies that are type-hinted in this method's signature:
<?php
namespace App\Console\Commands;
use App\User;
use App\DripEmailer;
use Illuminate\Console\Command;
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'email:send {user}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send drip e-mails to a user';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @param \App\DripEmailer $drip
* @return mixed
*/
public function handle(DripEmailer $drip)
{
$drip->send(User::find($this->argument('user')));
}
}
Closure Commands
Closure based commands provide an alternative to defining console commands as classes. In the same way that route Closures are an alternative to controllers, think of command Closures as an alternative to command classes. Within the commands
method of your app/Console/Kernel.php
file, Laravel loads the routes/console.php
file:
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
Even though this file does not define HTTP routes, it defines console based entry points (routes) into your application. Within this file, you may define all of your Closure based routes using the Artisan::command
method. The command
method accepts two arguments: the command signature and a Closure which receives the commands arguments and options:
Artisan::command('build {project}', function ($project) {
$this->info("Building {$project}!");
});
The Closure is bound to the underlying command instance, so you have full access to all of the helper methods you would typically be able to access on a full command class.
Type-Hinting Dependencies
In addition to receiving your command's arguments and options, command Closures may also type-hint additional dependencies that you would like resolved out of the service container:
use App\User;
use App\DripEmailer;
Artisan::command('email:send {user}', function (DripEmailer $drip, $user) {
$drip->send(User::find($user));
});
Closure Command Descriptions
When defining a Closure based command, you may use the describe
method to add a description to the command. This description will be displayed when you run the php artisan list
or php artisan help
commands:
Artisan::command('build {project}', function ($project) {
$this->info("Building {$project}!");
})->describe('Build the project');
Defining Input Expectations
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.
Arguments
All user supplied arguments and options are wrapped in curly braces. In the following example, the command defines one required argument: user
:
/**
* The name and signature of the console command.
*
* @var string
*/
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}
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:
/**
* The name and signature of the console command.
*
* @var string
*/
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:
/**
* The name and signature of the console command.
*
* @var string
*/
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