Laravel 10 Create Custom Artisan Command Example - TechvBlogs

Laravel 10 Create Custom Artisan Command Example

In this article, You will learn How to Create and Use Custom Artisan Command in Laravel 10.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

7 months ago

Artisan is a command-line interface (CLI) included with Laravel that provides a convenient way to interact with your Laravel application. Laravel provides a number of built-in Artisan commands that can be used to perform common tasks, such as generating boilerplate code, migrating databases, and seeding data.

In addition to the built-in commands, Laravel also allows you to create your own custom Artisan commands. This can be useful for automating repetitive tasks, or for providing custom functionality specific to your application.

1. Install Laravel

To install Laravel 10, follow these steps:

Requirements: Before installing Laravel 10, make sure your system meets the following requirements:

  • PHP 8.2.0
  • Composer
  • MySQL, PostgreSQL, SQLite, or SQL Server database driver

Install Laravel: You can install Laravel 10 using Composer. Open your terminal or command prompt and run the following command:

composer create-project laravel/laravel custom-command

2. Database Configuration

In this step, we need to add database configuration in .env file. so let's add the following details and then run the migration command:

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=custom-command
DB_USERNAME=root
DB_PASSWORD=password

Next, run the migration command to create users table.

php artisan migrate

3. Generate Artisan Command

In this step, we need to create CreateUsers a class using the following command. Then copy the below code into it. we will add create:users a command name.

php artisan make:command CreateUsers

Then let's update the following command file.

app/Console/Commands/CreateUsers.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create Dummy Users for your App';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $numberOfUsers = $this->argument('count');
  
        for ($i = 0; $i < $numberOfUsers; $i++) { 
            User::factory()->create();
        }

        return Command::SUCCESS;
    }
}

4. Use Created Artisan Command

So, let's run the following custom command to create multiple users:

php artisan create:users 1
  
php artisan create:users 50

You can check in your user's table, it will create records there.

Next, you can check your custom command on the list as well.

php artisan list

 Thank you for reading this article.

Comments (0)

Comment


Note: All Input Fields are required.