website icon Felipe Mateus

How to create commands in Laravel?

First today I'm going to talk about how to make commands in Laravel that serve to create crons or to execute commands directly from the terminal. In other words, all created commands appear in the list when you run artisan.

List of “php artisan” commands in the image shows the command “example:command” which is a test command.

Creating the command in Laravel

Create a new class for the command the class will have the variable $signature which is the variable with the signature used in the artisan command. As well as the variable $description which is used to describe the commands.

In this sense, you will also need to declare the function handle() used to perform the actions of the command.

Variable Declaration $signature
Variable Declaration $description
Occupation handle()

creating provider

In addition, it is necessary to create a class provider which will be responsible for importing the commands into the application. However if you are creating outside of a package you need to import in class kernel.php from the Laravel project.

Boot Provider

Function example boot() where to import the Commands in Laravel from Example

Kernel Console

Kernel console import example

Likewise, if you are creating a new package, don't forget to import the provider in app.php and not composer.json.

Command template in Laravel

Check out more about how the package and how to install and how to create a command in laravel in addition to also having the source code all in the link below.

If you liked this post, don't forget to share it with your friends, it will help me a lot.


Subscribe to my newsletter to receive updates on new posts and new projects. Stay updated.

Exit mobile version