Laravel Custom Artisan Command

In this article, we will discuss Laravel Custom Artisan Command. Laravel provides a command-line interface which is known as Artisan. It provides lots of helpful commands which help us at the time of development.

Are you familiar with the Laravel, if not then you can start with given tutorials?

You can use list command to view all available Artisan commands.

php artisan list

After executing this command, the list of available commands shown in the terminal. All of the commands make our development easy. You can create auth, controller, model, mail, migration and many more. But the question is can we make our own artisan command. And the answer is yes, you can create your own artisan command for any kind of your logic.

Steps to Create Custom Artisan Command

Laravel provides an easy way to create the custom artisan command. We can use the given command to create a command.

php artisan make:command <command_name>

Open your terminal and run the below-given command to create custom Artisan command.

php artisan make:command CreateAdminAccount

That command creates a file in the app/Console/Commands directed with the name of CreateAdminAccount.php which looks like:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CreateAdminAccount 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, time to update the newly generated command. We’ll set $signature to create:admin and $description to Create a user account with admin role. But you have to update “role” field in your user table.

After this, if you run php artisan list command in your terminal but the command still not exists in the list. Its because it has not been registered. To register the command, we need to update the Kernel.php file located at app/Console. It should look like this:

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

After updating the Kernel.php, our newly created command is ready for use. You can check this using php artisan list.

Till our command doing nothing, because still, we are not adding any logic in our command. So open the command file and update the handle function.

First, we have to create the migration for the role as mentioned in the command description.

php artisan make:migration add_role_in_users --table=users

Our migration file is ready and available in database/migrations directory. Open the migration file and update as like given code.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddRoleInUsers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('role')->comment('Available roles Admin or User');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('role')->comment('Available roles Admin or User');
        });
    }
}

Now update your User Model.

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

Now update the newly generate command.

<?php

namespace App\Console\Commands;

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

class CreateAdminAccount 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 user account with admin role. But you have to update "role" field in your user table.';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $newuser = [
            'name' => 'Tony Stark',
            'email' => 'tonystark@domain.com',
            'password' => bcrypt('secret'),
            'role' => 'Admin'
        ];
        $user = User::create($newuser);
        if($user) {
            echo 'New User "tonystark@domain.com" and password "secret" is successfully created.';
        }
    }
}

After updating the command, You can run php artisan create:admin in the terminal.

Conclusion

In this article, we will discuss Laravel Custom Artisan Command. Here, we create a simple basic example for the artisan command. You can create more advanced or complex command to make your development faster. Also as per my experience, I’m using the custom commands for my CRON jobs. We will discuss this in our future articles. Hope you like this article, please feel free to add the comments if any query.

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

ArtisanArtisan CommandLaravelLaravel5Laravel5.6PHP Artisan
Comments (0)
Add Comment