June, 15 2020
The CLI( Command Line Interface) is one of the best places to execute a long long-running task and Laravel has provided us with the Artisan Console that allows us to create our custom commands. I usually set up custom artisan commands whenever I am building something that requires initial setup or when I want to run queries on a large DB.
In this tutorial, I am going to show you how to set up a custom Artisan command and how to calculate the execution time of that command.
There are two ways to create a custom Artisan Console command, the Class-Based Command and the Closure Based Command.
The class base command will require us to set up a class for our artisan command (this class can be generated with the Artisan make:command
). These custom command classes reside in the app/Console/Commands
directory of your Laravel application.
The Closure-Based Command resides inside the routes/console.php
files in your Laravel application.
For this tutorial, we will use the Closure Based Command.
//routes/console.php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Artisan;
Artisan::command('send:emails', function(){
$this->comment('Processing');
app(UserController::class)->sendEmails();
$this->comment('Processed');
});
This is a closure based commands that send out emails. Add this to the console.php
file in the routes
directory of your laravel application.
Now we can run artisan send:email
.
php artisan send:emails
Something you might need to accept CLI arguments for your custom command. This is very simple with Laravel. The same way we define route arguments is how we do it for a custom command.
Artisan::command('send:emails {type} {delay}', function($type, $delay){
$this->comment('Processing');
app(UserController::class)->sendEmails($type, $delay);
$this->comment('Processed');
});
The arguments are enclosed with curly brackets and passed to the closure by type-hinting as the closure function parameters.
Running the command will now require we pass the email $type
and the also a $delay
.
php artisan send:emails active 10
To calculate the execution time of a custom command we can leverage the Laravel now()
helper method. The now()
helper returns an instance of Carbon, now we can leverage the diffInSeconds()
or diffInMinutes()
methods depending on what you want to return.
First, we have to define the start time and calculate the difference between the start and end time of the execution.
//routes/console.php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Artisan;
Artisan::command('send:emails', function(){
$start = now();
$this->comment('Processing');
app(UserController::class)->sendEmails();
$time = $start->diffInSeconds(now());
$this->comment("Processed in $time seconds");
});
Here is a sample output.
Now you know about the custom artisan command you can now move your initial setup actions or long running task in your Laravel applications to the console.
follow me on twitter.
Be the first to hear about anything I publish, launch, or think is helpful for you. Subscribe here
Litehost is a web hosting platform for PHP & Laravel developers with Composer, Git, PHP & CLI pre-installed. Try it now