How to Create Custom Artisan Command in Laravel ?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this example i will show you How to Create Custom Artisan Command in Laravel ? Here i will create the custom command for create the new admin user. So this custom command will ask your user name, email and also password. Now in this example we will create the command as like this.

It’s better in the event that we make our own artisan command for like project setup, create new administrator client and so on in our laravel application. In the event that we create custom command, we can make project setup like create one administrator client, run movement, run seeder and so on In this post I give you model, how to create console commands in laravel project.

Custom command
php artisan admins:create

okay now by using this command we can create the new admins. So at first need to fire the bellow command and then create the console class file.

php artisan make:console AdminCommand --command=admins:create

And after this command we can find one file as AdminCommand class in the console directory. So one of that file and put the bellow code.

app/Console/Commands/AdminCommand.php

namespace App\Console\Commands;


use Illuminate\Console\Command;
use Hash;
use DB;


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


    /**
     * 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()
    {
        $input['name'] = $this->ask('What is your name?');
        $input['email'] = $this->ask('What is your email?');
        $input['password'] = $this->secret('What is the password?');
        $input['password'] = Hash::make($input['password']);


        DB::table('admins')->insert($input);
        $this->info('Admin Create Successfully.');
    }
}

Okay good, now we just need to register this command as a class in the kernel.php file, so need to open file and add the class on this way:

app/Console/Kernel.php
namespace App\Console;


use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;


class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\AdminCommand::class,
    ];
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        

    }
}

Now hope we are ready to the use or custom command, just we need fire the bellow command and then check we can find command in the list on this way “admins:create”.

php artisan list
php artisan admins:create

Read Also: Generate Captcha code and Validation by using BotDetect package in laravel

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

About code chef

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 code chef →