How To Setup A Custom Artisan Command And Calculate The Execution Time
Tags: php, laravel, artisan, console
Published: 2020-07-01 08:02:52

---
title: How To Setup A Custom Artisan Command And Calculate The Execution Time
published: true
date: 2020-06-29 21:01:02 UTC
tags: php,laravel,artisan,console
canonical_url: https://medium.com/@stephenjudeso/how-to-setup-a-custom-artisan-command-and-calculate-the-execution-time-66323055b80e
---

![](https://cdn-images-1.medium.com/max/600/1*SagjVmyLJejjGdjEK0WLJg.png)

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.

### Creating Custom Command

```php
//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.

```bash
php artisan send:emails
```

### Custom Commands With Arguments

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.

```php
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 .

```bash
php artisan send:emails active 10
```

### Calculating Execution Time

To calculate the execution time of a custom command we can leverage the Laravel now() [helper method](https://laravel.com/docs/7.x/helpers#method-now). 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.

```php 
//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.

![](https://cdn-images-1.medium.com/max/832/0*y7ERSQDHvULYNjUi.png)

Now you know about the custom artisan command, you can now move your initial setup actions or long-running tasks that are in your Laravel applications to the console.

Join my [mailing list](https://stephenjude.me/newsletter) and follow me on [twitter](https://twitter.com/stephenjudeso).