How To Create Custom Artisan Command in Laravel 8 Tutorial

Share this Article
Reading Time: 7 minutes
6,448 Views

Laravel artisan provides a command palette to operate with laravel application. It is a command line tool which gives application access by means of commands. We can create like controllers, components, models, migrations etc.

In case if we need some custom command in artisan command list. This tutorial will be the perfect guide to create artisan command.

Inside this article we will see step by step guide to create custom artisan command in laravel 8. We will have only few simple steps to do this.

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Laravel Artisan Command Panel

Open project into terminal and type this artisan command to see all available commads.

$ php artisan

This command will list all available commands in artisan panel. Inside this list we should see a command called make:command. Have a look into this image.

make:command is used to create artisan command.

When we create custom command, then command files will be stored at /app/Console/Commands folder. Initially, Commands folder will not be inside folder structure, but when we create command files, it will create that.


Command #1 – Create Basic Artisan Command

We will create a simple basic command which asks for user information and simply displays all information into console.

Run this command to create command file.

$ php artisan make:command UserInfo

Command file UserInfo.php will be created inside /app/Console/Commands folder.

Open UserInfo.php and write this code into it.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This is the command asks for user information and display';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $name = $this->ask('What is your name?');

        $email = $this->ask('What is your email address?');

        $this->info('User Informations: Name - ' . $name . " and Email - " . $email);
    }
}
  • $signature – It gives command syntax to artisan panel.
  • $description – Description of command
  • $this->info – It provides a information line at console

Usage of Command

Back to terminal and run this command

$ php artisan user:info

Command #2 – Use of Model into Command File

By default in Laravel application, there is a migration file for users table. Let’s run migrate command create tables into database.

$ php artisan migrate

This command will create a users table in database.

Also we have a User.php model file inside /app/Models.

Custom Command For Create User

Back to terminal and run this spark command.

$ php artisan make:command UserCreate

It will create a file UserCreate.php into /app/Console/Commands folder.

Open UserCreate.php and write this command.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use App\Models\User;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This is the command asks for user information and create into database';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $input['name'] = $this->ask('What is your name?');

        $input['email'] = $this->ask('What is your email address?');

        $input['password'] = $this->ask('Provide your secret password?');

        $input['password'] = Hash::make($input['password']);

        User::create($input);

        $this->info("User created successfully");
    }
}

Usage of Command

Back to terminal and run this command

$ php artisan user:create

This command will asks for user information like name, email & password and then it will create user after that.

We hope this article helped you to learn about How To Create Custom Artisan Command in Laravel 8 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5