How To Create Custom Command In Laravel 9

Websolutionstuff | Feb-14-2023 | Categories : Laravel

In this article, we will see how to create a custom command in laravel 9. Here we will learn about how to make custom artisan commands in laravel 8 and laravel 9. Laravel provides its own artisan commands for creating migration, models, controllers, etc. You can build your own custom commands.

Commands are typically stored in the app/Console/Commands directory. To create a new command, you may use the make:command Artisan command. This command will create a new command class in the app/Console/Commands directory.

Learn More: Artisan Console

So, let's see laravel 9 make a custom artisan command, how to create a custom command in laravel 8, laravel command, and php artisan make:command.

Step 1: Install Laravel 9

In this step, we will install the laravel 9 application using the following command.

composer create-project laravel/laravel laravel9-command

 

Step 2: Configure Database

In this step, we will configure the database in the .env file. So, add the following details to that file.

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel9_command
DB_USERNAME=root
DB_PASSWORD=root

After that, migrate the table into the database using the artisan command.

php artisan migrate

 

 

Step 3: Make Artisan Command

Now, we will make a custom artisan command using the below command.

php artisan make:command CreateUsers

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;
    }
}
 
Step 4: Use Artisan Command

Now, we will use the custom artisan command. So, run the following command to the terminal.

php artisan create:users 5
  
php artisan create:users 15

After that, you can check your custom artisan command on the list using the following command.

php artisan list

Output:

how_to_create_custom_artisan_command_in_laravel_9

 


You might also like:

Recommended Post
Featured Post
How to Create Login and Registration in Laravel 11
How to Create Login and Regist...

Hello developers! In this guide, We'll see how to create a login and registration page in laravel 11 using the larav...

Read More

Apr-15-2024

How To Push Array Element In Node.js
How To Push Array Element In N...

Hello Dev, In this example we will see how to push array element in node.js example. I will share some example about&...

Read More

Oct-11-2021

PHP - file_get_contents() SSL Connection Reset by Peer
PHP - file_get_contents() SSL...

Hey there! So, you're working with PHP and trying to fetch content from an HTTPS URL using the file_get_contents() f...

Read More

Mar-01-2024

How To Backup Database In Laravel 9 Using Spatie
How To Backup Database In Lara...

In this article, we will see how to back up the database in laravel 9 using spatie. Here, we will learn automatic&n...

Read More

Feb-08-2023