Create Custom Artisan Command Laravel 9 Example

Today we are going to learn how to Create Custom Artisan Command Laravel 9 Example. This tutorial will cover Custom Artisan Command Laravel 9 with example.

In addition to the commands provided with Artisan, you may build your own custom commands. Commands are typically stored in the app/Console/Commands directory; however, you are free to choose your own storage location as long as your commands can be loaded by Composer.

Steps to Create Custom Artisan Command Laravel 9 Example:

  • Step 1: Installing fresh new Laravel 9 Application
  • Step 2: Creating Database and Configuration
  • Step 3: Generating Artisan Command
  • Step 4: Using Created Artisan Command
  • Step 5: Output
  • Step 6: Conclusion

Also Read: How to Store JSON in Database Laravel 9

Step 1: Installing fresh new Laravel 9 Application

First step is to install a fresh new Laravel 9 application. To install a laravel application run the following command in terminal.

composer create-project laravel/laravel custom-artisan-app

cd custom-artisan-app

Note:custom-artisan-app” is the laravel application name.

Installing fresh new Laravel 9 Application
Installing fresh new Laravel 9 Application

Step 2: Creating Database and Configuration

Now in this step we are creating a database and configuring a database. First create a database by opening you phpmyadmin and create a new database.

Creating Database
Creating Database

Also Read: Enum Model Attribute Casting in Laravel 9 Example

Then you need to configure the database details in laravel application. Open .env file and enter the database details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=custom-artisan-app
DB_USERNAME=root
DB_PASSWORD=
Database Configuration
Database Configuration

Now run the migration to create a tables in the database by following command.

php artisan migrate
Database Migration - Create Custom Artisan Command Laravel 9 Example
Database Migration

Also Read: Livewire Pagination Laravel 9 Example Tutorial

Step 3: Generating Artisan Command

Now, we are going to create a CreateUsers command class for the application. Run the following code to create a CreateUsers command.

php artisan make:command CreateUsers

Then let update the code with the following.

app/Console/Commands/CreateUsers.php

<?php
  
namespace App\Console\Commands;
  
use Illuminate\Console\Command;
use App\Models\User;
  
class CreateUsers extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:users {count}';
  
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create Dummy Users for your App';
  
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $numberOfUsers = $this->argument('count');
  
        for ($i = 0; $i < $numberOfUsers; $i++) { 
            User::factory()->create();
        }  
  
        return 0;
    }
}
Generating Artisan Command - Create Custom Artisan Command Laravel 9 Example
Generating Artisan Command

Also Read: Restrict User Access from IP Address in Laravel 9 Tutorial

Step 4: Using Created Artisan Command

So Now, we are using created artisan command to create a dummy user with this command run the following code.

php artisan create:users 12
  
php artisan create:users 6

Change the number to how many dummy user you want to create.

Using Created Artisan Command - Create Custom Artisan Command Laravel 9 Example
Using Created Artisan Command

Step 5: Output

You can find the command in the list:

php artisan list
Create Custom Artisan Command Laravel 9 Example
Create Custom Artisan Command Laravel 9 with Example

Step 6: Conclusion

Today, We had learn Create Custom Artisan Command Laravel 9 Example. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: How to create SEO friendly sluggable URL Laravel 9 Example

3 Comments

Leave a Reply