How To

How to Create Custom Artisan Command in Laravel 8?

Hello Laravel Developers,

In this Laravel tutorial, we will learn How to Create Custom Artisan Command in Laravel 8? Before that, letโ€™s understand Artisan in Laravel.

What is Artisan in Laravel?

Artisan is a command line interface that is included with Laravel. Artisan provides several helpful commands that can assist you in building your application. To view all available Artisan commands, you may use the list command as follows:

$ php artisan list

This article will show how to create custom artisan commands in Laravel.

Typically commands are stored in the app/console/Commands directory.

Before starting, we need some pre-requirements listed below:

  1. New Laravel project.
  2. A secure database connection.
  3. And User model.

How to Create Custom Artisan Command in Laravel:

Step 1: First, we will create a command using artisan console or cmd. We need to use the make:command Artisan command. This command will create a new command class in the app/console/commands directory.

$ php artisan make:command checkUser

Step 2: After that, define the appropriate values of $signature and $description properties of the class. The handle method will be called when command is executed. You may place your command logic in this method. Letโ€™s take a look at our checkUser command file.ย 

<?php

namespace App\Console\Commands;

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

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

    /**
     * The console command description.
     *
     * @var string
     */    protected $description = 'This command is used to check whether an email address exists in the database or not.';

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

    /**
     * Execute the console command.
     *
     * @return int
     */    public function handle()
    {
        $email = $this->argument('email');
        $user = User::where('email', $email)->first();
        if ($user)
        {
            $this->info("User is exist");
        }
        else
        {
            $this->info("User not exist");
        }
        return 0;
    }
}

Letโ€™s test our command in the console

The below output shows if the user exist

The below output shows if the user does not exist

Conclusion:

This way, you can create any of your required custom Artisan command in Laravel 8. If you have any doubt, let me know through the comment section. Get in touch with our Laravel Experts to expand your knowledge in Laravel.

Happy Coding!

Click to rate this post!
[Total: 6 Average: 4.8]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ๐Ÿ… with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess โ™Ÿ๏ธ or Cricket ๐Ÿ.

Recent Posts

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

4 hours ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

5 hours ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

2 days ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

5 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 week ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 week ago