How To Create Custom Artisan Command In Laravel 10

  Dec 2023
  ITSolutionsGuides
  Category: Laravel
How To Create Custom Artisan Command In Laravel 10

Hi Developers,

Lets see How To Create Custom Artisan Command In Laravel 10. We will create the custom artisan command with a suitable example for the better undestanding of the custom artisan command. In Laravel framework we can create our own artisan command to perform any sort of the functions like creating migration, models, controllers etc. Today we will create the custom artisan command to create the dummy users using the factory setting. We can generate dummy users by simply executing the custom artisan command we created. So lets quickly learn How To Create Custom Artisan Command In Laravel 10.

Learn to craft personalized Artisan commands in Laravel 10. Elevate your development by harnessing the power of custom commands for efficient workflows.

Let's Start Coding

Lets create the new laravel project using the following command,
laravel new custom_artisan_command
then lets run the migrations using the following artisan command,
php artisan migrate

We are going to make custom artisan command for creating the dummy users using the factory settings , lets create the custom artisan command using the following atisan command

php artisan make:command CreateUsers

the above artisan command will create the file in the path app/Console/Commands/CreateUsers.php in this file we will declare the actions to be performed during the execution of the command, $signature will determine the structure of the artisan command here we will also add {count} as the parameter since we are using the for loop for executing we are getting how many times the loop should be executed from the artisan command itself. In $description will display the message or the information or the short message about the artisan command that will display during route:list command.

In the handle() we will get the parameter enter by the user to determine the number of the executions of the for loop. In the for loop we will use the factory settings to add the dummy users.


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.
     */
    public function handle(): void
    {
        $numberOfUsers = $this->argument('count');
    
        for ($i = 0; $i < $numberOfUsers; $i++) { 
            User::factory()->create();
        }
    }
}

Let's Start Coding

Now we have created the custom artisan command and also declared the fucntions that needed to be performed during the execution of the artisan command we created. Lets run the artisan command, Since we have created the for loop to run the factory settings we need to also send the second parameter to declare how many times the loops should be executed.

php artisan create:users 100

Let's Start Coding

The below artisam command will display all the artisan command available and its functions, and also displays the custom artisan command we generated and its function, So lets see functions of the artisan command we created,

php artisan list

create:users                Create Dummy Users for your App


// displays the $signature and $description of the artisan command

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides