How To Create Custom Artisan Command in Laravel 8 Tutorial

Share this Article
Reading Time: 7 minutes
2,886 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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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.

Find More on Laravel 8 Articles here