How To Create a Custom Laravel Command?

How To Create a Custom Laravel Command?

Commands are a great way to harness the power of Laravel in a seamless way. While Laravel comes out of the box with its own set of commands, we can also add our own custom commands in our Laravel Project. Using these commands, we can then trigger various functionalities that we wish to.

How To Create a New Laravel Command?

For us to use a command, we will have to first make that command. Creating a command is pretty straightforward, using Artisan. To create a new custom command, we will use the following artisan command:

php artisan make:command LogWrite

By default, this will create a new PHP file at app/Console/Commands directory. This location can be customized if you want to but we are not going to do that here.

Populating the Laravel command details

Open the YourCommandName.php file and what you will see there will be pretty self-descriptive. However, let’s go through it here anyways.

The $signature variable is where you will define the signature of the command, preferably, in the format given. In our case, we will do the following:

protected $signature = 'log:write';

Next, you will have to give it a short description in the $description variable. This is what you will see when you run php artisan list command on the terminal. Let’s see how that looks in the terminal.

Our newly created command on php artisan list

Our newly-created is not ready to be run. But, we haven’t told it anything about what it needs to do when executed. So, let’s go ahead and do that.

Writing The Logic To Execute

The handle function is the place where all that needs to be executed, has to be added.

Let’s go ahead and add something very simple and see how it all works. Open your app/Console/Commands/LogWrite.php file and scroll towards the bottom to find handle function.

We will modify the handle function to look like this:

public function handle()
{
   info('Hello from the log:write command');
}

What we are doing here is, writing a line of text inside the Laravel Log file. We will see where it is actually gets written in a moment. First let’s see how we can execute/run the command that we just created.

How To Run a Laravel Command?

In order to run our newly created command and see how it works, we will run the following command in the terminal (while being inside our project’s root directory):

$ php artisan log:write
Executing the command at the terminal

As you can see in the screenshot above, there were no errors after calling the command via the terminal. So, we are sure that the command ran successfully. But, did it do what it was supposed to do? Let’s go and check that now.

In order to do that, open the \your_project_root\storage\logs\laravel.log.

And sure enough, there will be this line of text there, Hello from the log:write command, prepended by the date and time when it was written.

Result of successful execution of our Laravel command

That’s all it takes to create a custom Laravel command.

Share This Post

Subscribe To my Future Posts

Get notified whenever I post something new

More To Explore

How To Use Google SIgn In In Flutter
Blog

How To Use Google Sign In Flutter?

This topic is divided into 2 parts. The parts are as follows: 1 – Creating a Firebase Project 2 – Configuring the Flutter app and