Creating CLI commands in Laravel
By Flavio Copes
Learn how to create your own Artisan CLI commands in Laravel with php artisan make:command, defining the command signature and its handle() method.
In this Laravel series we’ve used Artisan, the Laravel command line tool, to perform various actions:
php artisan servephp artisan make:migrationphp artisan migratephp artisan make:modelphp artisan make:controllerphp artisan breeze:install
Those are all built-in commands.
There are many, many more.
Some you’ll use often, some you’ll never use.
Run php artisan to see them all with a short explanation:

And to see how to use a command in particular, run php artisan <command> -h:

You can create your own commands, too.
Run
php artisan make:command MyCommand
This creates a file in app/Console/Commands/MyCommand.php pre-filled with some code. Since Laravel 13 the command name and description are PHP attributes on the class (up to Laravel 12 they were the $signature and $description properties):
<?php
namespace App\Console\Commands;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
#[Signature('app:my-command')]
#[Description('Command description')]
class MyCommand extends Command
{
/**
* Execute the console command.
*/
public function handle()
{
//
}
}
If you have commands written with the protected $signature and protected $description properties, they still work in Laravel 13.
#[Signature('app:my-command')] defines how the command will be called, in this case you can run it using
php artisan app:my-command
In the handle() method you’ll write the code that runs when the command is executed.
public function handle()
{
//
}
The simplest code could be printing something to the console, for example:
public function handle()
{
$this->info('test!');
}
Now try running php artisan app:my-command:

You can do lots of things in a command. You can accept arguments, interactively ask something to the user using prompts to confirm or asking for input, or let them choose between different options, you can format output in different ways…
Commands are great to perform one-off tasks, maintenance, and much more. Inside a command you have access to all the goodies provided by Laravel, including your own code, classes, and more.
You can also call other commands. And commands can be ran by any part of your Laravel app.
You can also schedule commands using schedules. The server can be configured to run Laravel’s schedules, and then any schedule configured in Laravel will be executed as needed.
The official Artisan docs cover arguments, options and scheduling in detail. This post is part of The Laravel Guide.
Want me to talk about your product? You can sponsor this site.
Related posts about laravel: