DEV Community

Sadiul Hakim
Sadiul Hakim

Posted on

PHP Laravel Artisan Custom Command

What is Laravel Artisan?

Artisan is Laravel’s command-line interface (CLI).

It lets you:

  • Run framework commands (migrate, route:list, cache:clear)
  • Generate code (make:model, make:controller)
  • Create your own commands for automation, scripts, cron jobs, maintenance, etc.

Run it from your project root:

php artisan
Enter fullscreen mode Exit fullscreen mode

You’ll see a big list of commands — your custom commands live here too.


Why create custom Artisan commands?

Custom commands are great for:

  • Data imports
  • Maintenance scripts
  • Batch jobs
  • Cron jobs
  • Admin tools
  • One-time scripts
  • Developer utilities

Think of them as backend CLI tools for your app.


Creating a Custom Command

Laravel gives you a generator.

php artisan make:command GreetUser
Enter fullscreen mode Exit fullscreen mode

This creates:

app/Console/Commands/GreetUser.php
Enter fullscreen mode Exit fullscreen mode

Anatomy of a Command

Open the file

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GreetUser extends Command
{
    // The command name & signature
    protected $signature = 'greet:user';

    // Description shown in `php artisan list`
    protected $description = 'Greet a user via the console';

    // Command logic
    public function handle()
    {
        $this->info('Hello from Laravel!');
    }
}
Enter fullscreen mode Exit fullscreen mode

Run it:

php artisan greet:user
Enter fullscreen mode Exit fullscreen mode

Boom. Your first custom command.


Command Signature (Arguments & Options)

The signature defines how the command is called.

Basic argument

protected $signature = 'greet:user {name}';
Enter fullscreen mode Exit fullscreen mode

Run:

php artisan greet:user Sadiul
Enter fullscreen mode Exit fullscreen mode

Access it:

$name = $this->argument('name');
Enter fullscreen mode Exit fullscreen mode

Optional argument

protected $signature = 'greet:user {name?}';
Enter fullscreen mode Exit fullscreen mode

Option (flags)

protected $signature = 'greet:user {name} {--shout}';
Enter fullscreen mode Exit fullscreen mode

Run:

php artisan greet:user Sadiul --shout
Enter fullscreen mode Exit fullscreen mode

Check:

if ($this->option('shout')) {
    // do something
}
Enter fullscreen mode Exit fullscreen mode

Taking Text Input (Interactive)

Laravel has built-in prompts (very nice UX).

Ask for input

$name = $this->ask('What is your name?');
Enter fullscreen mode Exit fullscreen mode

Ask with default

$name = $this->ask('Your name?', 'Guest');
Enter fullscreen mode Exit fullscreen mode

Ask secretly (passwords)

$password = $this->secret('Enter password');
Enter fullscreen mode Exit fullscreen mode

Confirm (Yes / No)

if ($this->confirm('Do you want to continue?')) {
    $this->info('Continuing...');
} else {
    $this->warn('Cancelled');
}
Enter fullscreen mode Exit fullscreen mode

Multiple Choice (Single Select)

$role = $this->choice(
    'Select a role',
    ['Admin', 'Editor', 'User'],
    2 // default index
);
Enter fullscreen mode Exit fullscreen mode

User sees:

Select a role:
  [0] Admin
  [1] Editor
  [2] User
Enter fullscreen mode Exit fullscreen mode

Output Helpers (Very Important)

$this->info('Success message');
$this->warn('Warning message');
$this->error('Error message');
$this->line('Plain text');
Enter fullscreen mode Exit fullscreen mode

Tables

$this->table(
    ['ID', 'Name'],
    [
        [1, 'Alice'],
        [2, 'Bob'],
    ]
);
Enter fullscreen mode Exit fullscreen mode

Full Sample Command (REALISTIC)

Create it

php artisan make:command CreateUserCli
Enter fullscreen mode Exit fullscreen mode

Code: app/Console/Commands/CreateUserCli.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CreateUserCli extends Command
{
    protected $signature = 'user:create 
                            {name? : User name}
                            {--admin : Create as admin}';

    protected $description = 'Create a user via CLI';

    public function handle()
    {
        // Argument or prompt
        $name = $this->argument('name') 
            ?? $this->ask('Enter user name');

        // Email input
        $email = $this->ask('Enter email');

        // Password input
        $password = $this->secret('Enter password');

        // Role selection
        $role = $this->choice(
            'Select role',
            ['Admin', 'Editor', 'User'],
            2
        );

        // Confirmation
        if (! $this->confirm('Create this user?')) {
            $this->warn('Operation cancelled');
            return Command::FAILURE;
        }

        // Simulate save
        $this->info('User created successfully!');
        $this->table(
            ['Field', 'Value'],
            [
                ['Name', $name],
                ['Email', $email],
                ['Role', $role],
            ]
        );

        return Command::SUCCESS;
    }
}
Enter fullscreen mode Exit fullscreen mode

Run it

php artisan user:create
Enter fullscreen mode Exit fullscreen mode

or

php artisan user:create Sadiul --admin
Enter fullscreen mode Exit fullscreen mode

Exit Codes (Pro Tip)

return Command::SUCCESS; // 0
return Command::FAILURE; // 1
Enter fullscreen mode Exit fullscreen mode

Useful for cron jobs & scripts.


Where Commands Are Registered?

Laravel auto-registers commands in:

app/Console/Kernel.php
Enter fullscreen mode Exit fullscreen mode

Unless you’re doing something custom — you usually don’t touch this.


Common Real-World Uses

  • php artisan import:csv
  • php artisan cleanup:logs
  • php artisan sync:users
  • php artisan system:health-check
  • php artisan cron:run-jobs

Top comments (0)