Laravel Create and use Custom Artisan Command
Hello Artisans, today I'll talk about how you can create your own custom command in your Laravel Application. As we'll know Laravel comes with Artisan command-line interface, which makes the development life cycle very easy. We'll see how we can create some dummy users using our own created Artisan command. So let's see how we can create and use our custom artisan command in our Laravel Application.
Note: Tested on Laravel 9.2.
Table of Contents
Setup .env
Frist of all we need to ensure that our database is connected. So, insert database credentials like database name, username and password in your .env file as below.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=generate_command
DB_USERNAME=root
DB_PASSWORD=
Create and Setup Artisan Command
Now we need to create a command using the below command.
php artisan make:command createDummyUsers
It'll create a file under the app/Console/Commands/createDummyUsers.php, open the file and paste the below code
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
class createDummyUsers extends Command
{
protected $signature = 'create:dummy-users {number}';
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$usersData = $this->argument('number');
for ($i = 0; $i < $usersData; $i++) {
User::factory()->create();
}
}
}
Output
Now you are all set to go, just fire the below command in your terminal
php artisan create:dummy-users 50
Hola!!! your users are created:

That's it for today. Hope you'll enjoy this tutorial. You can also download this tutorial from GitHub. Thank's for reading :)
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)