How to Create Custom Artisan Command in Laravel

How to Create Custom Artisan Command in Laravel

Laravel Artisan commands are one of the most practical tools for automating repetitive tasks, enforcing standards, and extending application functionality from the command line. If you’ve ever needed to generate files, clean up data, trigger background processes, or standardize internal workflows, learning how to create custom artisan command in Laravel is essential.

In this guide, you’ll learn how to create, register, and run a custom artisan command in Laravel, including advanced use cases such as running artisan commands from a controller.

What Is an Artisan Command in Laravel?

Artisan is Laravel’s built-in command-line interface. It ships with dozens of commands for tasks like migrations, caching, queues, and code generation. A custom artisan command in Laravel allows you to define your own CLI commands that run application logic securely and consistently.

Common use cases include:

  • Automating recurring backend tasks

  • Generating boilerplate files

  • Running maintenance or cleanup jobs

  • Triggering business logic without UI interaction

  • Executing background processes via cron

Why Create a Custom Artisan Command in Laravel?

Creating your own command gives you:

  • Cleaner architecture (logic outside controllers)

  • Reusability across environments

  • Safer execution for sensitive operations

  • Better automation for DevOps and cron jobs

This is why Laravel encourages developers to move operational logic into Artisan commands instead of controllers or routes.

How to Create Custom Artisan Command in Laravel (Step-by-Step)

Step 1: Generate a New Artisan Command

Use Laravel’s built-in generator:


 

php artisan make:command GenerateReport

This creates a new file at:


 

app/Console/Commands/GenerateReport.php

This is the foundation of your Laravel create command artisan workflow.

Step 2: Understand the Command Structure

Open the generated file:


 

<?php namespace App\Console\Commands; use Illuminate\Console\Command; class GenerateReport extends Command { protected $signature = 'report:generate'; protected $description = 'Generate a daily system report'; public function handle() { $this->info('Report generated successfully.'); } }

Key parts:

  • $signature → command name used in terminal

  • $description → shown in php artisan list

  • handle() → command execution logic

This is the core of a custom artisan command in Laravel.

How to Run Your Custom Artisan Command

Once created, run it using:


 

php artisan report:generate

If registered correctly, Laravel will execute the handle() method.

How to Add Arguments and Options to Artisan Commands

Most real-world commands need parameters.

Example: Adding an Argument


 

protected $signature = 'report:generate {date}';

Access it inside handle():


 

$date = $this->argument('date');

Run it like:


 

php artisan report:generate 2026-01-01

Example: Adding an Option


 

protected $signature = 'report:generate {--email=}';

Access it:


 

$email = $this->option('email');

Run it:


 

php artisan report:generate --email=admin@example.com

This makes your custom artisan command in Laravel PHP far more flexible.

Writing Real Logic Inside handle()

Your handle() method can contain:

  • Database queries

  • Service class calls

  • API requests

  • File system operations

Example:


 

public function handle() { $users = \App\Models\User::where('active', true)->count(); $this->info("Active users: {$users}"); }

For clean architecture, always move heavy logic into service classes, then call them from the command.

Laravel Run Artisan Command From Controller (When Needed)

Sometimes you need to run artisan command Laravel controller logic, such as triggering a command from an admin dashboard.

How to Run Artisan Command From Controller


 

use Illuminate\Support\Facades\Artisan; Artisan::call('report:generate');

With arguments:


 

Artisan::call('report:generate', [ 'date' => now()->toDateString(), '--email' => 'admin@example.com', ]);

This approach allows you to Laravel run artisan command from controller safely.

Best practice:
Only run artisan commands from controllers for admin-only or internal tools, never public routes.

Scheduling Custom Artisan Commands (Cron Automation)

Laravel’s scheduler makes automation simple.

Open:


 

app/Console/Kernel.php

Add:


 

protected function schedule(Schedule $schedule) { $schedule->command('report:generate')->daily(); }

Then ensure your server cron runs:


 

* * * * * php /path/to/project/artisan schedule:run

This is one of the most common reasons developers create custom artisan commands in Laravel.

Best Practices for Custom Artisan Commands

1. Keep Commands Thin

Commands should orchestrate, not contain heavy business logic.

2. Use Dependency Injection

Inject services via constructor instead of using static calls.

3. Validate Arguments Early

Fail fast if required data is missing or invalid.

4. Use Console Output Properly


 

$this->info('Success'); $this->error('Failure'); $this->warn('Warning');

This improves debugging and logging.

Common Mistakes to Avoid

  • Putting business logic directly in controllers instead of commands

  • Running artisan commands from public routes

  • Writing long procedural logic inside handle()

  • Not documenting command usage

When to Use Artisan Commands vs Controllers

Use Case Artisan Command Controller
Cron jobs ✅ Yes ❌ No
Background tasks ✅ Yes ❌ No
Admin automation ✅ Yes ⚠️ Limited
Public API actions ❌ No ✅ Yes

Final Thoughts

Learning how to create custom artisan command in Laravel is a fundamental skill for any serious Laravel developer. Artisan commands help you automate workflows, maintain clean architecture, and run critical backend operations safely.

Whether you’re generating files, cleaning data, or running scheduled jobs, a well-designed custom artisan command in Laravel keeps your application maintainable and scalable.

If you’re building enterprise Laravel systems or automation-heavy platforms, mastering Artisan commands is non-negotiable.

Relevant Guides & Services

What is Custom Ecommerce Software Development

Multichannel Retail ERP

Cloud ERP for Retail

Pharma Retail ERP Software

Footwear Retail ERP

Headless ERP for Retail

Sign up for our newsletter