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
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
This creates:
app/Console/Commands/GreetUser.php
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!');
}
}
Run it:
php artisan greet:user
Boom. Your first custom command.
Command Signature (Arguments & Options)
The signature defines how the command is called.
Basic argument
protected $signature = 'greet:user {name}';
Run:
php artisan greet:user Sadiul
Access it:
$name = $this->argument('name');
Optional argument
protected $signature = 'greet:user {name?}';
Option (flags)
protected $signature = 'greet:user {name} {--shout}';
Run:
php artisan greet:user Sadiul --shout
Check:
if ($this->option('shout')) {
// do something
}
Taking Text Input (Interactive)
Laravel has built-in prompts (very nice UX).
Ask for input
$name = $this->ask('What is your name?');
Ask with default
$name = $this->ask('Your name?', 'Guest');
Ask secretly (passwords)
$password = $this->secret('Enter password');
Confirm (Yes / No)
if ($this->confirm('Do you want to continue?')) {
$this->info('Continuing...');
} else {
$this->warn('Cancelled');
}
Multiple Choice (Single Select)
$role = $this->choice(
'Select a role',
['Admin', 'Editor', 'User'],
2 // default index
);
User sees:
Select a role:
[0] Admin
[1] Editor
[2] User
Output Helpers (Very Important)
$this->info('Success message');
$this->warn('Warning message');
$this->error('Error message');
$this->line('Plain text');
Tables
$this->table(
['ID', 'Name'],
[
[1, 'Alice'],
[2, 'Bob'],
]
);
Full Sample Command (REALISTIC)
Create it
php artisan make:command CreateUserCli
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;
}
}
Run it
php artisan user:create
or
php artisan user:create Sadiul --admin
Exit Codes (Pro Tip)
return Command::SUCCESS; // 0
return Command::FAILURE; // 1
Useful for cron jobs & scripts.
Where Commands Are Registered?
Laravel auto-registers commands in:
app/Console/Kernel.php
Unless you’re doing something custom — you usually don’t touch this.
Common Real-World Uses
php artisan import:csvphp artisan cleanup:logsphp artisan sync:usersphp artisan system:health-checkphp artisan cron:run-jobs
Top comments (0)