Artisan Console
Introduction
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
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
Laravel Sail
If you are using Laravel Sail as your local development environment, remember to use the sail
command line to invoke Artisan commands. Sail will execute your Artisan commands within your application's Docker containers:
./vendor/bin/sail artisan list
Tinker (REPL)
Laravel Tinker is a powerful REPL for the Laravel framework, powered by the PsySH package.
Installation
All Laravel applications include Tinker by default. However, you may install Tinker using Composer if you have previously removed it from your application:
composer require laravel/tinker
Looking for hot reloading, multiline code editing, and autocompletion when interacting with your Laravel application? Check out Tinkerwell!
Usage
Tinker allows you to interact with your entire Laravel application on the command line, including your Eloquent models, 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"
The dispatch
helper function and dispatch
method on the Dispatchable
class depends on garbage collection to place the job on the queue. Therefore, when using tinker, you should use Bus::dispatch
or Queue::push
to dispatch jobs.
Command Allow List
Tinker utilizes an "allow" 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
, migrate:install
, up
, and optimize
commands. If you would like to allow more commands you may add them to the commands
array in your tinker.php
configuration file:
'commands' => [
// App\Console\Commands\ExampleCommand::class,
],
Classes That Should Not Be Aliased
Typically, Tinker automatically aliases classes as you interact with 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\Models\User::class,
],
Writing Commands
In addition to the commands provided with Artisan, you may 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, you may 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 - it will be created the first time you run the make:command
Artisan command:
php artisan make:command SendEmails