Get In Touch
Diamond Marg, Kupondole heights ,
Lalitpur-10 ,Nepal,
Ph: +977 980-1039816
Work Inquiries
Ph: +977 980-1039816

Creating Custom Command in Laravel

Laravel provides numerous artisan commands for automating tasks such as controller creation, database migration, server initialization, etc. But sometimes you may need custom commands for custom solutions as per your special needs. Laravel offers flexibility beyond its built-in commands, allowing users to create their own with relative ease.

  1. Generate Command: Use Artisan’s ‘make:command’ command to generate a new command. Open your terminal and run:

php artisan make:command ActiveUsers

Now the command file is created inside the app > Console > Commands folder. The file name will be ActiveUsers.php

2. Update Signature Property: Update the $signature property of the command like this:

protected $signature = ‘active:users’;

For consistency and readability, the kebab case is often recommended for custom command names in Laravel.

3. Update Description Property: Description increases the readability of the command. It describes what the commands do.

protected $description = ‘Get active users on the platform’;

Write Logic On handle() Method: Now you can write all logic that the command should do on the handle() method.

public function handle()
  $activeUsers = User::where(‘is_active’, 1)->get();
  return $activeUsers;

  1. Run Command: Now You can run the command like this:

php artisan active:users

You may pass additional arguments to the command.

protected $signature = ‘active:users ​​{limit}’;

You can also make it optional by appending a question mark at the end.

protected $signature = ‘active:users ​​{limit?}’;

You may also set a default value for an argument.

protected $signature = ‘active:users ​​{limit=10}’;

These arguments can be captured by using:


This generates an associative array where the arguments serve as keys and their corresponding values as values. For instance, to retrieve the value of the limit argument, you can access it like this:

$limit = $this->arguments()[‘limit’];

Or if there is only one argument, you can do like this:

$limit = $this->argument(‘limit’);

Thats it. Now you know about creating the custom command in the Laravel. Next time I will come up with another topic in Laravel. Thank you.

Nirjal Prasad Wagle
Nirjal Prasad Wagle

Leave a Reply

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

This website stores cookies on your computer. Cookie Policy