How to Create Custom Artisan Command in Laravel?

Hello Laravel Developers,

In this Laravel blog, we will learn How to Create Custom Artisan Command in Laravel 8. Before that, let’s understand Artisan in Laravel.

Artisan is a command-line interface (CLI) included with Laravel that provides a convenient way to interact with your Laravel application. Laravel provides several 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 the user’s 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 code below 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

Second Method

We’ll look at how to make a custom command in Laravel. With the help of Artisan, we can increase the speed of making Laravel applications. Laravel uses a tinker package for a command-line interface. We can customize the output of our command.
Artisan is the command-line interface included with Laravel. It will help you build the Laravel application. To check the all-artisan command, you can run the below command:

php artisan list

You can find a description of a particular command. You need to add the ‘help’ keyword before the actual command.

php artisan help migrate

Tinker:-

By default, Laravel includes the Tinker package. It is a powerful REPL for the laravel framework. you can also install the manual package using Composer:

composer require Laravel or Tinker

Tinker allows you to interact with your entire laravel application on the command line. To enter the Tinker environment, run the Tinker command.

php artisan tinker

You can publish Tinker’s configuration file using the ‘vendor:publish’ command.

php artisan vendor:publish –provider=”Laravel\Tinker\TinkerServiceProvider”

You can add commands to execute on its shell. We may add them inside the command array in your tinker.php configuration file.

‘commands’ => [
// App\Console\Commands\ExampleCommand::class,
],

Tinker makes alias classes automatically as you require them. You add classes in the dont_alias array of your tinker.php configuration file.

‘dont_alias’ => [
App\User::class,
],

Writing commands: –

You can create a custom command for your application. It will be stored inside the app/console/commands directory.

Generating command:

You make a new custom command using the make:command Artisan command. It will create a command class in the app/console/commands directory.

php artisan make:command SendEmails

Command structure:

After executing the above command, you will see the signature and description properties of the class. The handle method will execute when your command is executed. You can inject any dependencies we need. The Laravel service container will automatically inject all dependencies that are type-hinted in this method’s signature.

<?php
namespace App\Console\Commands;

use App\DripEmailer;
use App\User;
use Illuminate\Console\Command;

class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = ’email:send {user}’;

/**
* The console command description.
*
* @var string
*/
protected $description = ‘Send drip e-mails to a user’;

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

/**
* Execute the console command.
*
* @param \App\DripEmailer $drip
* @return mixed
*/
public function handle(DripEmailer $drip)
{
$drip->send(User::find($this->argument(‘user’)));
}
}

Closure Commands:

Closure-based commands provide an alternative to defining console commands as classes. Laravel loads the routes/console.php file.

/**
* Register the Closure-based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path(‘routes/console.php’);
}

You may define all closure-based routes using the Artisan::command method.

Artisan::command(‘build {project}’, function ($project) {
$this->info(“Building {$project}!”);
});

Closures may also type-hint additional dependencies that you would like resolved out of the service container.

use App\DripEmailer;
use App\User;

Artisan::command(’email:send {user}’, function (DripEmailer $drip, $user) {
$drip->send(User::find($user));
});

You may use the describe method to add a description to the command.

Artisan::command(‘build {project}’, function ($project) {
$this->info(“Building {$project}!”);
})->describe(‘Build the project’);

Defining Input Expectations:

You can specify the input you expect from the user using a signature property on your command.

Argument:

We wrapped all arguments in curly braces, made arguments optional, and defined default values for the parameters.

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = ’email:send {user}’;

// Optional argument…
email:send {user?}

// Optional argument with default value…
email:send {user=foo}

Options:

Two hyphens prefix options (–) add when they define in the command line. Options that don’t receive a value serve as a Boolean ‘switch’.

/**
* The name and signature of the console command.
*
@var string
*/
protected $signature = ’email:send {user} {–queue}’;

If the -queue option passes to the command, the value will be true Otherwise, the value will be false.

php artisan email:send 1 –queue

You can also pass value to options.

/**
* The name and signature of the console command.
*
* @var string
*/

protected $signature = ’email:send {user} {–queue=}’;

You may assign default values to options by specifying the default value.

php artisan email:send 1 –queue=default

email:send {user} {–queue=default}

You can define a shortcut before the option name and use the | delimiter to separate.

email:send {user} {–Q|queue}

Input Array:

You want to pass the array input to the command. You may use the * character.

email:send {user*}
php artisan email:send foo bar

When defining an array that expects an array, we should prefix the input of each option value passed to the command with the option name:

email:send {user} {–id=*}

php artisan email:send –id=1 –id=2

Command I/O:-

Retrieving Input:

You need to access the values for arguments and options accepted by your command.

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$userId = $this->argument(‘user’); //
}

To get all the arguments, call the arguments method:

$arguments = $this->arguments();

You can retrieve options using the option method. To retrieve all options as an array, call the options method.

// Retrieve a specific option…
$queueName = $this->option(‘queue’);

// Retrieve all options…
$options = $this->options();

Prompting for input:

You may ask the user for the execution of your command. The ask method will prompt the user with a question.

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$name = $this->ask(‘What is your name?’);
}

Users’ input will not be visible when they type on the console.

$password = $this->secret(‘What is the password?’);

You want to get confirmation from the user. You may use the confirmation method.

if ($this->confirm(‘Do you wish to continue?’)) {
//
}

The anticipate method enables you to automatically complete potential choices.

$name = $this->anticipate(‘What is your name?’, [‘Taylor’, ‘Dayle’]);

You can add Closure as a second argument to the anticipate() method. The closure should accept a string parameter containing the user’s input so far and return an array of options for auto-completion:

$name = $this->anticipate(‘What is your name?’, function ($input) {
// Return auto-completion options…
});

If you need to give the user a predefined set of choices, you may use the choice method.

$name = $this->choice(‘What is your name?’, [‘Taylor’, ‘Dayle’], $defaultIndex);

You can also pass a fourth and fifth parameter to the choice method, which determines the maximum number of attempts to select a valid response, and multiple selections are permitted.

$name = $this->choice(
‘What is your name?’,
[‘Taylor’, ‘Dayle’],
$defaultIndex,
$maxAttempts = null,
$allowMultipleSelections = false
);

writing output:

You can write the output on the console using line, info, comment, question, and error methods. Each method uses ANSI colors for its purpose.

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info(‘Display this on the screen’);
}

Also, you can use the table method. It dynamically calculated the width and height based on the data. You should pass the header and rows.

$headers = [‘Name’, ‘Email’];
$users = App\User::all([‘name’, ’email’])->toArray();
$this->table($headers, $users);

It is helpful to show the progress bar for the long-running task. We can use the start, advance, and stop methods to show a progress bar.

$users = App\User::all();
$bar = $this->output->createProgressBar(count($users));
$bar->start();
foreach ($users as $user) {
$this->performTask($user);
$bar->advance();
}
$bar->finish();

Registering Commands:

It automatically registered all commands with an artisan from the app/console/commands directory. There may be additional calls to the load method to scan another directory.

/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(DIR.’/Commands’);
$this->load(DIR.’/MoreCommands’); // …
}

You may manually register commands by adding their class name to the $commands property of the app/console/kernel.php file. It will automatically resolve when artisan boots.

protected $commands = [
Commands\SendEmails::class
];

Programmatically Executing commands: –

You may fire an Artisan command from the controller. You may use the call method on the Artisan facade. Command name or class as the first argument and an array of command parameters as the second argument. You can also pass the direct artisan command to call method.

Route::get(‘/foo’, function () {
$exitCode = Artisan::call(’email:send’, [
‘user’ => 1, ‘–queue’ => ‘default’
]);
//
});

Artisan::call(’email:send 1 –queue=default’);

You may even queue Artisan commands, so your queue worker processes them in the background.

Route::get(‘/foo’, function () {
Artisan::queue(’email:send’, [
‘user’ => 1, ‘–queue’ => ‘default’
]);
//
]);

Artisan::queue(’email:send’, [
‘user’ => 1, ‘–queue’ => ‘default’
])->onConnection(‘redis’)->onQueue(‘commands’);

Calling commands from other commands:

The call method accepts the command name and an array of parameters.

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->call(’email:send’, [
‘user’ => 1, ‘–queue’ => ‘default’
]); //
}

You want to call another console command and suppress its output. You can use the callSilent() method.

$this->callSilent(’email:send’, [
‘user’ => 1, ‘–queue’ => ‘default’
]);

Stub Customization:-

They generate these classes using “stub” files that populate with values based on your input. You may sometimes wish to change files generated by Artisan. Execute the below command to create stubs for customization.

php artisan stub:publish

The published stubs will be within a stubs directory in the root directory. It will reflect any changes you make to these stubs when you generate their corresponding classes using Artisan make commands.

I hope that this post (How to Make a Custom Command in Laravel) has helped you understand how to customize the output of a command, register commands, and programmatically execute commands. Please leave a comment if you have any queries, and I will answer as quickly as possible.

Thank you for reading this blog. Please share this blog. That’s it for the day. Stay Connected!

Sorry, you must be logged in to post a comment.

Translate »