How to create a custom artisan command in laravel? (Examples)
Laravel is a popular PHP framework that allows developers to build scalable and robust web applications. Laravel’s command-line interface, Artisan, provides a simple and efficient way to manage various tasks such as database migrations, unit testing, and more. In this guide, I will walk you through the process of creating a custom Artisan command in Laravel.
Let’s create a custom artisan command in laravel with the below step by step guide.
Step 1: What is Artisan Command?
Before we dive into creating a custom command, let’s understand the basics of Laravel’s Artisan command. Artisan is a command-line interface that provides several built-in commands that can be executed from the terminal. To list all the available commands, run the following command in your terminal:
php artisan list
This will show a list of all available Artisan commands that you can execute in your Laravel application.
Step 2: Creating Custom Artisan Command
To create a custom Artisan command in Laravel, we need to use the make:command
command. This command generates a new command class in the app/Console/Commands
directory. Run the following command in your terminal:
php artisan make:command RefreshContent
RefreshContent is just a name for my custom artisan command, you can name it anything.
This will generate a new file called RefreshContent.php
in the app/Console/Commands
directory. This file contains a skeleton command class that we can modify to create our custom command.
Step 3: Define The Command Signature and Description
Before we start implementing our custom command logic, let’s define the command signature and description. The signature is the name of the command that we will use in the terminal to execute our custom command, for example, db:seed
, migrate
, serve
, storage:link
are some of the predefined command’s signatures.
We use these signatures as below,
php artisan db:seed
php artisan migrate
And so on…
The description is a brief description of what the command does. Modify the $signature
and $description
variables in the RefreshContent.php
file as follows:
protected $signature = refresh:content {argument};
protected $description = 'This is a custom command.';
In this example, we have set the command signature to refresh:content
and added an argument called {argument}
, but argument is optional you can skip this. We have also set the description to “This is a custom command.”
So, now we can use our command as below,
php artisan refresh:content any-argument
Step 4: Implementing Command Logic
Now that we have defined the signature and description of our custom command, let’s implement the command logic. In this example, we will simply print the value of the argument passed to our command. Modify the handle method in the RefreshContent.php
file as follows:
public function handle() { $argument = $this->argument('argument'); $this->info('The value of the argument is '.$argument); }
In this example, we are retrieving the value of the {argument}
passed to our command and printing it to the console using the info method.
Step 5: Testing the Custom Command
To test our custom command, run the following command in your terminal:
php artisan custom:command test
This will execute our custom command and print “The value of the argument is test” to the console.
We can also check the list of available commands and our custom command will be there now.
Step 6: Final code for custom artisan command
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class RefreshContent extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'refresh:content {arg1}'; /** * The console command description. * * @var string */ protected $description = 'This is a custom command!'; /** * Execute the console command. * * @return int public function handle() { $argument = $this->argument('arg1'); $this->info('This command has the argument: '.$argument); } }
Summary:
In this guide, I have walked you through the process of creating a custom Artisan command in Laravel. I have covered the basics of Artisan command, creating a custom command, defining the command signature and description, implementing the command logic, and testing the custom command.
With this knowledge, you can now create custom Artisan commands that automate repetitive tasks, integrate with third-party APIs, and more. I hope this guide has been helpful and informative, and I wish you all the best in your Laravel development journey.