How to create custom artisan command in laravel 11

Creating custom Artisan commands in Laravel 11 is a fundamental skill for automating tasks and extending your application’s functionality through the command line. Here’s a step-by-step guide:

1. Generate the Command Class

Laravel provides a convenient Artisan command to generate the boilerplate for your custom command. Open your terminal in your Laravel project root and run:

php artisan make:command MyCustomCommand

Replace MyCustomCommand with the desired name for your command. It’s common practice to use a PascalCase name for the class. This will create a new file in app/Console/Commands/

Path: E:\xampp\htdocs\projects\laravel\1\my-laravel-app\app\Console\Commands\testcmdcustom.php

Customize the signature and put the logic or info in handle function:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

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

    /**
     * Execute the console command.
     */
    public function handle()
    {
       $this->info('Custom command executed successfully!');
    }
}

Run the command using terminal

php artisan app:testcmdcustom   //which you define for protected $signature = 

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *