Skip to content

Creating CLI commands in Laravel

In this Laravel series we’ve used Artisan, the Laravel command line tool, to perform various actions:

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:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MyCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:my-command';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        //
    }
}

$signature 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.


→ Get my Laravel Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about laravel: