Introduction to Artisan Commands

Learn the basics of Artisan commands and predefined command-line operations in Laravel.

Overview of Artisan concepts in Laravel

Artisan is an effective way of communicating technical aspects to Laravel. With the help of CLI commands, developers can interact with the application. Almost all the frameworks introduce a command line-based interface to interact with.

Artisan primarily focuses on providing a set of predefined commands that assist developers in performing common tasks within the Laravel framework. The most important among them includes the following:

  • Generation of controllers

  • Creation and execution of queue jobs

  • Manipulation of view structure

  • Cache clearance

  • Middleware development

Developers are also given the opportunity to create their own logic by registering a custom Artisan command.

Basic usage of Artisan commands

By accessing the command prompt of the operating system and changing the directory to the installed path of the Laravel app, developers can execute commands using a generic structure.

Php artisan <list of available or custom-defined statments>

To illustrate all the commands using the coding widget below, follow these steps:

  1. Click the “Run” button.

  2. Move toward the terminal.

  3. Press “Ctrl + C” after the configuration of the setup command.

  4. Execute the php artisan list command.

Bud1�icdsclboolpublicdsclbool	resourcesdsclboolroutesdsclboolstoragedsclbool @� @� @� @E�DSDB ` @� @� @
Playground for a basic Artisan list

We can illustrate all the routes present in the application using the coding widget below. The following steps need to be carried out:

  1. Click the “Run” button.

  2. Move toward the terminal.

  3. Press “Ctrl + C” after the configuration of the setup command.

  4. Execute the php artisan route:list command.

Bud1�icdsclboolpublicdsclbool	resourcesdsclboolroutesdsclboolstoragedsclbool @� @� @� @E�DSDB ` @� @� @
Playground for routes listing

Laravel has a mechanism to store the most recently used configuration in the cache. But sometimes, new configurations and states are required in order to display recent changes. An example of clearing the cache from the Laravel application is given below.

Press + to interact
Command to clear the application cache
Command to clear the application cache

The following command will start the Laravel development server and the application will be accessible at 8000 or the next available port into the computer.

Press + to interact
Command to start the Laravel application
Command to start the Laravel application

Artisan execution with code

Sometimes, there’s a need to execute Artisan commands with the help of code due to the unavailability of shell access. The execution of Artisan commands with code is necessary when the code is deployed to a shared hosting environment or when a cron job is executed dynamically.

In Laravel, we can run Artisan commands programmatically using the Artisan facade or the Illuminate/Console/Command class. Here’s an example of how we can run an Artisan command in our code:

<?php
use Illuminate\Support\Facades\Artisan;
$exitCode = Artisan::call('command:name', $arguments, $outputBuffer);
?>
Artisan execution without command line access

Developers can utilize the Artisan facade, as mentioned above, in shared hosting environments in many ways, such as during deployment. Often, applications require clearing the cache, but most shared hosting environments don’t provide support for shell access. By using cache:clear in the Artisan:call() function, the unused cache can be cleared without having shell access.