How to Create Custom Artisan Command in Laravel 8 ?

Share Me
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares
How to Create Custom Artisan Command in Laravel 8
How to Create Custom Artisan Command in Laravel 8

Hello Dev’s Today in this blog,I will give you a example of how to create custom artisan command in laravel application. Here we will know how to create custom artisan command in laravel 8 for insert dummy data in database.

When we are working with a  new and fresh application at that time we must need some dummy data for our testing purpose. So at that we will use our new custom artisan command to insert our dummy data in database.

So just follow below step to custom artisan command:

Install Laravel 8

Here we will create laravel 8 new application.

composer create-project --prefer-dist laravel/laravel CreateDummyUser

Create Command

Now in this step we will create a command. So, first need to open our terminal and run bellow command.

php artisan make:command createDummyUser

app/Console/Commands/createDummyUser.php

Now need to update our command file like as below :

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\User;

class createDummyUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:user {user}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $totalCreateUser = $this->argument('user');

        for ($i = 0; $i < $totalCreateUser; $i++) { 
            factory(User::class)->create();
        }
    }
}

Run Command

So now run our command and at last of the command pass total number of user that we want to create.

below command will create 10 dummy user.

php artisan create:user 10

Read Also : Laravel 8 Login with Google Account Example

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →