How to Create Custom PHP Artisan Command in Laravel 9

Laravel PHP Custom Artisan command tutorial; Imagine a PHP developer’s life without the php artisan prefix, we are not sure about you but we would have been lazy without it.

There are tons of php artisan commands which automate package installation tasks and save hell lot of time. It makes command line interface related work utterly effortless.

In this example of laravel custom artisan command, we will explain how to create a custom artisan command in the Laravel app for creating the dummy data int the database from absolute starting.

Artisan is the name of the command-line interface incorporated with Laravel and driven by powerful Symfony Console. It gives several important commands for your use while developing a laravel web application.

Laravel 9 Custom PHP Artisan Command Example

  • Step 1: Create Laravel New App
  • Step 2: Update Database Details
  • Step 3: Migrate Default Tables
  • Step 4: Generate Artisan Class
  • Step 5: Create New Custom Artisan Command
  • Step 6: Run Command to Create Data
  • Step 7: Display Existing Commands Collection

Create Laravel New App

Usually, the first step begins by installing the new laravel app; if you don’t know the process, run the provided command.

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

Update Database Details

Secondly, add the database credentials in the .env file and connect the app to the database.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=db_username
DB_PASSWORD=db_password

Migrate Default Tables

We will be adding some records in the database for that we need few tables in the database.

Luckily, laravel comes with default migration tables; you can run the artisan migrate command for migrations.

php artisan migrate

Generate Artisan Class

For making a custom command, we need a separate class, and you can create the artisan command class by using the artisan make command.

php artisan make:command createUsers

Create New Custom Artisan Command

This section will show you how to configure a newly generated artisan class hence open app/Console/Commands/createUsers.php file then import user model, define the command name, command description, and the query to create the users.

<?php

namespace App\Console\Commands;
use Illuminate\Console\Command;

use App\Models\User;


class createUsers extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:insert-records {num}';

    /**
     * The console command description.
     */
    protected $description = 'Generate users data';

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

    /**
     * Run console command.
     */
    public function handle()
    {
        $addRecords = $this->argument('num');

        for ($i = 0; $i < $addRecrods; $i++) { 
            User::factory()->create();
        }
    }
}

Run Command to Create Data

The last section will show you how to generate new user records and insert them into the database.

php artisan create:insert-records 555

How to Create Custom PHP Artisan Command in Laravel

Display Existing Commands Collection

Similarly, you can show or view all the available commands; from the console you may execute the list command.

php artisan list

Summary

What makes Laravel an amazingly powerful, brisk PHP framework is its speed, simplicity.

There are tons of traits that help in faster web development and directly quantify web development experience. The PHP artisan is the helping hand in laravel that adds profoundness in rapid web application development.

In this tutorial, we learned how to build a custom artisan command to create fake records in the database. However, you can create a custom artisan command in laravel for any other purpose.