Jan 29, 2025
Alanas M.
10min Read
Laravel is celebrated for its clean architecture and developer-friendly approach to web development. Yet, even in such a well-designed framework, tasks like creating files, managing databases, or debugging can slow you down.
That’s where Laravel Artisan comes in. This powerful command-line interface tool simplifies and automates these tasks, freeing you to focus on building exceptional features and delivering high-quality applications.
Laravel Artisan is the out-of-the-box command-line interface included in Laravel. It provides developers with a comprehensive suite of commands to generate code, manage databases, and optimize applications. By automating these tasks, Artisan helps you save time, maintain consistency, and streamline your workflows across projects.
Artisan works by using terminal commands, which allow you to interact with your Laravel project directly from the command line. Instead of manual actions, Artisan enables writing console commands that handle everything from scaffolding new code to performing maintenance tasks, making it an essential part of a Laravel developer’s toolkit.
Follow these steps to get started:
php artisan
If Laravel is installed, this will return a list of all available Artisan commands:

If the command throws an error, you will need to set up Laravel first.
cd path-to-your-project
Change path-to-your-project with the actual path of your Laravel project.
php artisan serve
You can add the –help flag to any command to view the information about a command, including its description and the command parameters:
php artisan [command-name] --help
These commands allow you to efficiently set up the main components of your application, saving you from manually creating boilerplate code:
php artisan make:model [ModelName]
php artisan make:controller [ControllerName]
php artisan make:request [RequestName]
php artisan make:resource [ResourceName]
php artisan make:factory [FactoryName]
php artisan make:seeder [SeederName]
These commands streamline the process of configuring your Laravel application, helping you manage databases, enforce request handling rules, and organize your project efficiently:
php artisan serve --host=[IP] --port=[port]
php artisan migrate
php artisan migrate:fresh
php artisan db:seed
php artisan migrate:rollback
php artisan make:middleware
php artisan storage:link
php artisan make:policy [PolicyName]
These commands act as tools to diagnose problems, allow interaction with the application in real-time, and verify that everything functions as intended, ensuring a smoother development process:
php artisan route:list
php artisan tinker
php artisan test
php artisan env
php artisan config:clear
php artisan view:clear
These commands help ensure your application is fast, reliable, and ready for production while also providing tools to manage downtime during updates:
php artisan optimize
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan cache:clear
php artisan queue:work
php artisan down
php artisan up
This is not an extensive list of all Artisan commands, as there are many more. However, these commands will likely be the main ones you interact with while using Laravel.

While built-in Artisan commands cover many scenarios, there are times when your project requires more specific functionality. Custom commands allow you to extend Laravel’s capabilities, turning repetitive or complex processes into reusable tools.
Some examples of when custom commands are particularly useful include:
Custom Artisan commands allow you to turn unique application needs into efficient, maintainable, and easily executable solutions, making your development process more streamlined and scalable.
Let’s break down the process of creating a custom Artisan command, from generating the file to testing it in your application.
To create a new command for Artisan, we start by generating the command file. Artisan has a helpful command to make this file quickly:
php artisan make:command MyCustomCommand
This will create a new file with a name matching the one you specified in the app/console/commands directory. For example, running the command above generates a file named MyCustomCommand.php.
Here’s how the generated new command class looks like:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MyCustomCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
//
}
}This file provides the structure you need to define your custom command. Here’s a quick breakdown of the key properties in the generated command class:
After generating your command, the next step is to give it a name, define any arguments or options it needs, and describe what it does. These are set in the $signature and $description properties.
In this example, we’ll create a command that sends a notification to a user:
protected $signature = 'user:notify {user_id} {--force}';
protected $description = 'Send a notification to a specific user, with an optional force flag';Syntax for Defining Arguments and Options
Setting these properties properly makes your command intuitive and effective in fulfilling its purpose.
The handle() method defines the core functionality of your custom command. This code runs every time the command is executed, making it the heart of your command’s functionality.
In our notification command example, here’s how the handle() method could look:
public function handle()
{
// Step 1: Retrieve argument and option method values
$userId = $this->argument('user_id'); // Get the user_id argument
$force = $this->option('force'); // Check if the --force option is used
// Step 2: Validate input
$user = User::find($userId); // Find the user by ID
if (!$user) { // Handle missing user
$this->error('User not found!'); // Write error message text
return Command::FAILURE; // Exit with failure exit code
}
// Step 3: Confirm action (if --force is not used)
if (!$force && !$this->confirm('Do you really want to notify this user?')) {
$this->info('Command cancelled.');
return Command::SUCCESS; // Exit cleanly as no error occurred
}
// Step 4: Perform the task
Notification::send($user, new CustomNotification()); // Send the notification
// Step 5: Exit gracefully
$this->info('Notification sent successfully!'); // Provide success feedback
return Command::SUCCESS; // Exit with successful exit code
}Step-by-step breakdown:
And now we have the entire Artisan command defined, just about ready for use.
After creating your custom command, you need to ensure it’s registered in Laravel so it can be executed. Laravel automatically registers commands stored in the app/Console/Commands directory. To confirm this, you can run:
php artisan list
If your command appears in the list, it’s already registered. However, if your command is stored elsewhere or doesn’t appear in the list, you’ll need to register it manually in the Kernel.php file.
The Kernel file, located in the app/Console directory, is a PHP file that acts as the central hub for managing Artisan commands. It tells Laravel which commands to load and ensures they are ready for use.
Beyond registering commands, Kernel.php also manages task scheduling, making it an essential part of your application’s command-line operations.
Registering your custom command manually
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}protected function commands()
{
$this->load(__DIR__.'/Commands');
// Explicitly register a command
$this->commands([
\App\Console\Commands\MyCustomCommand::class,
]);
require base_path('routes/console.php');
}
Important! Ensure that your class name defined in your custom command and your file name match. If they do not, Laravel’s autoloader won’t detect your command properly!

If your command doesn’t appear in the list after saving your changes, you may need to clear your cache by running php artisan optimize:clear.
Testing your custom command ensures it functions as intended and handles different scenarios correctly. Follow these steps:
php artisan user:notify 42
Replace 42 with a valid user ID to see if it works under the intended circumstances.
php artisan user:notify 42 --force
php artisan user:notify 99999
Based on our handle() method, this should return the error User not found!
Be sure to test your command thoroughly, especially in scenarios that mimic its intended real-world usage. Extensive testing ensures your command is reliable and meets the needs of your application.
Laravel Artisan is an essential tool for every Laravel developer. It helps you automate tasks, manage workflows, and simplify complex processes. Its built-in commands streamline development, while custom commands enable you to solve problems unique to your application.
By mastering Artisan, you’ll not only save time but also improve the quality and consistency of your code.
Some commands are php artisan make:model ModelName, which creates a new model, php artisan migrate, which runs migrations and php artisan route:list, which lists all the routes in your application.
You can use Composer to create a new Laravel project. An example command could be composer create-project laravel/laravel project-name, where project-name would be the name of your Laravel application.
To run migrations in Laravel, use php artisan migrate. This command applies all pending migrations to your database, creating or updating tables as defined in your migration files.
All of the tutorial content on this website is subject to Hostinger's rigorous editorial standards and values.