How to create custom artisan command in laravel 2020

HOW TO CREATE CUSTOM ARTISAN COMMAND IN LARAVEL

How to create custom artisan command in laravel 2020



In this article we are going to see. How we can create cutom artisan command in laravel 7. So Laravel out of the box provides some php artisan commands. You can check the list of these commands using this command in terminal.(Make sure you are in laravel project directory using terminal, Navigate to there before running this command).





php artisan list

Laravel provides these commands to help us in our development. But we can create some custom command to make our life easier while using Laravel.



Let’s See Steps To create custom artisan command

Watch Full Guide Step By Step

We can define command name very easily using this command.





php artisan make:command <command_name_goes_here>

So let’s create a user using php artisan custom command, We can run this command to define our custom command.



php artisan make:command CreateUserAccount
How to create custom artisan command in laravel

It will generate a file under “app/Console/Commands” directory. Have a look there.





How to create custom artisan command in laravel

This file will look some thing like this



<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CreateUserAccount 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 int
     */
    public function handle()
    {
        return 0;
    }
}

Set Command Name

Ok so we can set command name by setting up the signature. We can add something like





protected $signature = 'create:user';

It will display just like this in terminal. Want to add description.







Set Command Description

protected $description = 'Create a user account in Users table in database';

Add Logic to command

So we defined name and description but our command, Currently does nothing so let’s add some logic. In “Handle Function” add this code.



$newUser = [
            'name' => 'Manu',
            'email' => 'manu@zarx.biz',
            'password' => bcrypt('secret'),
            'role' => 'Admin'
        ];
        $user = User::create($newUser);
        if($user) {
            echo 'New User "Manu" created successfully.';
        }

//MAKE SURE ON THE TOP OF THE PAGE, TO CALL USER MODEL OTHERWISE YOU WILL GET ERROR
use App\User;

Register Custom Command

We created the command but we also need to register it with Laravel. So go to “app/Console/Kernel.php“. And add this code, Basically your command file





class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
       Commands\CreateUserAccount::class,
    ];
}

Test your command

So we created the logic, Now its time to see it we made it or there is any bug in our code so to check it, We can run our command. In terminal run



php artisan create:user

Check Output

How to create custom artisan command in laravel
How to create custom artisan command in laravel

So what you are planning to do with this Laravel feature. Leave a comment below.





%d bloggers like this: