How to Create Custom Artisan Command Using Laravel

In this post, we will learn how to create a custom artisan command in Laravel and how to insert records in database using custom command.


Step 1: Install Laravel

Step 2: Create Database Connection

Step 3: Create Laravel Artisan Command

Step 4: Run Artisan Command

Install Laravel :

Using composer we can install laravel , go in xampp, open CMD and run below command

composer create-project laravel/laravel --prefer-dist laravel-custom-command

Get in laravel application

cd laravel-custom-command

Create Database Connection

You have to connect your laravel application with Database using .env file.

Create Laravel Artisan Command

Using the “php artisan make:command” you can create a new custom command,

It will create a new file  inside the app/Console/Commands directory.

Run below command

php artisan make:command insertUsersinDb

Now you have to open app/Console/Commands/insertUsersinDb.php file, and define command name using $signature variable and you can pass the parameters like {anyparameter}

Using $description you can describe the custom command.

Put your logic inside handle() function

Run Custom Artisan Command

php artisan insert:records 4

Check Database

Leave a Reply

Your email address will not be published. Required fields are marked *