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 Laravel command 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

3 thoughts on “How To Create a Custom Laravel Command?”

  1. Pingback: How To Embed Facebook And Twitter Feed On Your Website? - Rajiv Verma

  2. Pingback: How To Schedule Jobs or Commands in Laravel? - Rajiv Verma

  3. Pingback: How To Insert Fake Data Into Table Using Laravel Factory? - Rajiv Verma

Leave a Comment

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

Subscribe To my Future Posts

Get notified whenever I post something new

More To Explore