How to create Laravel custom artisan command

In today article we will talk about How to Create Laravel custom artisan command. If you know php artisan migrate we will make our own. Laravel comes with many command utility to make developer life easier. From the basic php artisan serve to more essential like php artisan migrate all are made with artisan.

Overview

Laravel ships with a bunch of commands that aim to make your life as a developer easier. From generating models, controllers, middleware, test cases, and many other types of files for the framework.

You can get the list of available command by executing the following command

php artisan list

Before getting deep into creating command let see what is artisan.

What is php artisan?

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application. Developers use PHP Artisan to perform some important functional operations which include generating migrations, publishing package assets and many similar tasks.

Setup laravel

As we gonna use laravel so as prerequisite we need a working installation of laravel for the demo. If you do not have a working version of laravel you can follow our previous article on installing laravel.

Registering artisan command

Using Laravel, you can easily create custom artisan commands. Just type the following command in the Artisan Console to create a new custom command:

php artisan make:command <command-name>

We will create a command to add an admin user to our application. Open Terminal in the root of application and execute this command.

php artisan make:command CreateAdminUser

That command creates a file in the app/Console/Commands directed with the name of CreateAdminUser.php

Command location

And the basic CreateAdminUser.php should look like this.

<?php
 
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
 
class CreateAdminUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';
 
    /**
     * 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()
    {
        //
    }
}

Now, we gonna update the newly created Laravel command. We will define $signature to create admin and $description to create user account having admin role. Defining these, we also have to update the role field in the users table or whatever how you define your roles.

<?php
 
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
 
class CreateAdminUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:admin';
 
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new user with admin roles. Be sure you create it following your roles implementation';
 
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
 
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}

After updating the command we have to register our new created command in the Kernel.php. The file Kernel.php is located at app/Console/Kernel.php

 protected $commands = [
        Commands\CreateAdminUser::class,
    ];

Now you can execute the command to see your newly created command in the list php artisan list .

We gonna add the logical code to create the user in the handle function. In this article i assumed that my roles is just a column in my database. My Model fillable would look like this but it’s not necessary the same thing for you just follow your roles logical.

protected $fillable = [
   'name', 'email', 'password', 'role'
];

After updating the handle function our command should look like the following

<?php
 
namespace App\Console\Commands;
 
use App\User;
use Illuminate\Console\Command;
 
class CreateAdminUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:admin';
 
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new user with admin roles. Be sure you create it following your roles implementation';
 
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
 
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $user = User::create([
           'name' => 'Jhon Doe',
            'email' => 'admin@admin.com',
            'password' => bcrypt('password'),
            'role' => 'Admin'
        ]);
        if($user){
            $this->info('Admin user successfully created the email is admin@admin.com and the password is "password" ');
        }
    }
}

To excute our newly created command we just need to run in the terminal :

php artisan create:admin

Conclusion

Here we are at the end of the article How to Create Laravel custom artisan command. In this article we learn how we can create basic command that register an admin user but we can do much more. Using PHP Artisan, you can develop different commands depending upon the needs of your project. As usual if you have any questions regarding this article, or have any query related to Artisan command, feel free to share them in the comments’ section below. I will be happy to help you

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *