A custom Artisan command in Laravel refers to a command-line interface (CLI) tool that you can create to extend the functionality of the Artisan command-line tool provided by Laravel. Artisan commands are used for automating tasks, executing custom logic, performing database operations, and much more.
A custom Artisan command allows you to define your own command-line actions that are specific to your Laravel application. You can create a custom command to perform tasks that are not covered by the built-in Artisan commands. This gives you the flexibility to tailor the command-line interface according to your application’s requirements.
By creating a custom Artisan command, you can encapsulate a specific functionality or task within a command that can be executed using the php artisan
command. You can define the command’s name, description, and arguments, as well as handle the logic and operations associated with it.
To create a custom Artisan command in laravel, you can use the make:command
Artisan command provided by Laravel’s CLI. This command generates a new class file that extends Laravel’s Illuminate\Console\Command
class and includes the necessary methods for your command’s functionality. You can then define the command’s behavior and implementation within this class file.
So, In this tutorial, you will learn how to create and use a custom artisan command in laravel 10 apps.
Laravel 10 Create Custom Artisan Command Example
By using the following steps, you can create and use the custom artisan command in laravel 10 apps:
- Step 1: Setup New Laravel Project
- Step 2: Configure Database with Laravel Project
- Step 3: Create Custom Artisan Command
- Step 4: Add Code in Artisan Command
- Step 5: Run Artisan Command
Step 1: Setup New Laravel Project
First of all, open the terminal or command prompt.
Then execute the following command into it to download or install or download new laravel apps in your server:
composer create-project laravel/laravel --prefer-dist laravel-demo
Step 2: Configure Database with Laravel Project
Once you have installed laravel app. Now, you need to configure database with laravel apps.
So visit laravel app root directory and find .env file. And add the database details; as follows:
DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=laravel_db DB_USERNAME=root DB_PASSWORD=
Now execute the following command on command line to migrate Artisan command:
php artisan migrate
Step 3: Create Custom Artisan Command
To create a custom Artisan command, you can use the make:command
Artisan command provided by Laravel’s CLI
php artisan make:command generateUserData
Once you have created a custom artisan command. Now, you need to register artisan command in laravel app. So, Open the app/Console/Kernel.php
file and locate the commands
property. Add the fully qualified namespace of your generateUserData
class to the array:
protected $commands = [
\App\Console\Commands\generateUserData
::class,
];
Step 4: Add Code in Artisan Custom Command
Then visit app/Console/Commands/ directory and open generateUserData.php file. And update the following code into it:
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use App\Models\User; class generateUserData extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'create:generate-users {count}'; /** * The console command description. * * @var string */ protected $description = 'Creates test user data and insert into the database.'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return int */ public function handle() { $usersData = $this->argument('count'); for ($i = 0; $i < $usersData; $i++) { User::factory()->create(); } } }
Step 5: Run Artisan Command
Finally, you have to type the custom artisan command on the console screen and execute it on command line to generate the users’ data into the database:
php artisan create:generate-users 250
Conclusion
Congratulations! You have created a custom Artisan command in Laravel. You can further enhance your command by adding more arguments, options, or even interacting with external services to automate various tasks within your application.