Create Custom Artisan Command in Laravel 6

Laravel 6 , Laravel

Nicesnippets

350

21-11-2019


hii guys,

In this example,I will show you how to create custom artisan command in laravel 6 application. we will create custom artisan command to insert dummy data in database.

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

Follow below step to custom artisan command:

Install Laravel 6

you will create laravel 6 fresh application.

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

Create Command

In this step we will create command. So, first open your terminal and run bellow command.

php artisan make:command createDummyUser

app/Console/Commands/createDummyUser.php

update your 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

Now run your command and at last of the command pass number of user that you want to create.

below command will create 10 dummy user.

php artisan create:user 10

It will help you....


Recommended Posts