Search

Laravel Artisan: Interacting With Commands

December 7, 2016 —John Koster

This article will discuss the various ways that we as users can interact with Artisan console commands. This article contains many sections which focuses on specific ways to interact with commands or gain information about commands. Each section will examine an example signature, help output or input parameter type and then explain ways in which to supply data to the various input parameters.

#Executing a Command

To execute an Artisan command we first need to invoke the artisan console application. The console application is typically located in the root directory of the Laravel application and is named artisan. Some shells support invoking the application without specifying that it is a PHP script, the examples in this section will explicitly use PHP to invoke the artisan console application:

1# Invoking the Artisan console application.
2php artisan

When the above is executed Artisan outputs something similar to the following by default (the actual output will vary by version and if there have been any modifications made):

1Laravel Framework version 5.3.0-Dev
2
3Usage:
4 command [options] [arguments]
5
6...
7
8Available commands:
9 clear-compiled Remove the compiled class file
10 down Put the application into maintenance mode
11 env Display the current framework environment
12 help Displays help for a command
13 list Lists commands
14 migrate Run the database migrations
15 optimize Optimize the framework for better performance
16 serve Serve the application on the PHP development server
17 tinker Interact with your application
18 up Bring the application out of maintenance mode

To execute a specific Artisan command we must invoke the Artisan console application and then provide the name of the command we want to execute:

1php artisan <command_name>

If we wanted to execute the list command (the list command is executed by default when you do not specify a command name) we could enter the following in the shell to achieve similar output to the previous output:

1php artisan list

Since the Artisan console application is built on top of a Symfony console application it inherits all of the shortcuts that are available when interacting with Symfony console applications. One of the most useful shortcuts is the ability to not have to enter the full name of the command you want to execute. As long as the shorthand command name entered is not ambiguous (cannot be confused with another command name) it will be accepted. For example, since the list command is the only command that begins with the letter l by default all of the following would be equivalent and execute the list command:

1# Executing the list command
2php artisan l
3php artisan li
4php artisan list

Commands with namespaces can also be entered in this way. For example, the route:list command can easily be condensed into the following:

1php artisan r:l

If we had simply entered php artisan r: to execute a command Artisan would have throw an instance of Symfony\Component\Console\Exception\CommandNotFoundException. The error message would state something similar to "Command r: is ambiguous (route:cache, route:clear and 1 more)". The message is indicating that there is more than one command that the entered shortcut could apply to.

Here are a few more examples of using shortcuts when executing commands:

1# Creating a new User model.
2php artisan ma:mo User
3 
4# Cache the application's routes.
5php artisan r:ca
6 
7# Clear compiled views.
8php artisan vi:

#Entering Data

Most commands require some sort of input to process when the execute. The exact input that a command requires depends on the specifics of that command, but the way that input entered is consistent. As an example, let's take a look at the usage information for make:model Artisan command (obtained by executing the php artisan make:model -h command):

1Usage:
2 make:model [options] [--] <name>
3
4Arguments:
5 name The name of the class

As we can see the command expects the name of the new model class; this is displayed in angle brackets. The name of the command and the input parameters are separated by spaces like so:

1php artisan make:model User

That would execute the make:model command and pass the value User as the argument to the name parameter. Let's imagine for a second that we want to create the new model class but wanted to store it in a directory that has spaces in it. Since arguments are separated by spaces, we need to enclose the input value in quotes. If we do not enclose the input value in quotes we would receive an error stating something similar to "Too many arguments" or we would accidentally supply an incorrect value for a different input parameter.

1php artisan make:model "My Directory/User"

It should be stated that it is not recommended, nor is it useful, to store classes that need to be auto-loaded in directory names with spaces. This was only used as an example, but these techniques are useful for commands that accept names for arbitrary things, such as email addresses or user names.

#Supplying Array Data

Some commands accept multiple pieces of information to be stored in the same input argument; array parameters are defined as the last parameter in a commands signature so that there is no ambiguity between array data and other input parameters. Supplying data to array input parameters is very similar to supplying arguments to multiple input parameters (and the same rules of enclosing input that contains spaces in quotes applies).

Assuming a command with the signature:

1example:command {favoriteColors* : Your favorite colors}

We would supply our input arguments like so:

1php artisan example:command Blue Green "Blue Green"

The favoriteColors argument would then contain a value similar to the following:

1array:3 [
2 0 => "Blue"
3 1 => "Green"
4 2 => "Blue Green"
5]

Supplying data to array options is similar except that the name of the option must preface the intended value. For example, if the favoriteColors input parameter was an option we could have supplied the same values like so (line breaks have been added to improve readability):

1php artisan example:command --favoriteColors=Blue
2 --favoriteColors=Green
3 --favoriteColors="Blue Green"

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.