---
title: "Quick start — Metrics for Laravel | Roundly"
description: "Build a metric inline in one fluent expression with the Metrics facade, then read its typed result or return its JSON-ready envelope."
url: https://roundly-consulting.com/open-source/docs/metrics-for-laravel/quick-start
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Metrics for Laravel](https://roundly-consulting.com/open-source/docs/metrics-for-laravel.md)

# Quick start

Build a metric inline with the Metrics facade — no class required. Pick a type, an aggregate over an Eloquent query, and a range:

```php
use App\Models\User;
use RoundlyConsulting\Metrics\Enums\Period;
use RoundlyConsulting\Metrics\Facades\Metrics;

$data = Metrics::value()
    ->count(User::query())
    ->name('New users')
    ->range(Period::Today)
    ->withChangeAgainstPreviousPeriod()
    ->toArray();
```

The facade exposes one builder per metric type — Metrics::value(), Metrics::trend(), Metrics::progress() and Metrics::partition():

```php
Metrics::value()->count(User::query())->range(Period::Today)->toArray();
Metrics::trend()->count(User::query(), 'created_at')->daily()->range(Period::MonthToDate)->toArray();
Metrics::progress()->sum(Order::query(), 'total')->target(10000)->range(Period::MonthToDate)->toArray();
Metrics::partition()->count(User::query(), 'plan')->toArray();
```

## Reading a result

Every metric gives two views of the same calculation: result() — the typed result object (ValueResult, TrendResult, ProgressResult or PartitionResult) for PHP code that works with the numbers — and toArray(), the JSON-ready envelope for a front-end. Both go through the result cache:

```php
$revenue = Metrics::value()->sum(Order::query(), 'total')->range(Period::ThisMonth)->result();

$revenue->value();       // 15230.0
$revenue->change();      // percentage change, when a comparison was requested

Metrics::trend()->count(User::query(), 'id')->daily()->result()->values();   // list<float>
Metrics::get('revenue')->result()->value();   // the typed result of a registered metric
```

## The envelope

toArray() resolves the metric and returns a consistent envelope — presentation fields, the selected and available ranges, and the type-specific result:

```php
[
    'name' => 'New users',            // name(), or the humanized class name
    'description' => '',              // description()
    'prefix' => '',                   // prefix()
    'suffix' => '',                   // suffix()
    'range' => [
        'current' => 'TODAY',         // the selected Period value
        'available' => ['7' => '7 Days', '14' => '14 Days', /* … */ 'ALL' => 'All'],
        'custom' => ['start' => null, 'end' => null],
    ],
    'result' => [                     // the type-specific result
        'value' => 42.0,
        'previous' => 30.0,
        'change' => ['percentage' => 40.0, 'is_increase' => true],
    ],
]
```

Give inline metrics a name(): without one, the envelope’s name is the humanized class name, which for an inline builder is the builder itself (“Pending Value”).

## Return it from a controller

Every metric is Responsable, so a controller can return it directly and Laravel sends the envelope as JSON. Authorization stays your app’s responsibility:

```php
use App\Models\User;
use RoundlyConsulting\Metrics\Enums\Period;
use RoundlyConsulting\Metrics\Facades\Metrics;

final class NewUsersController
{
    public function __invoke()
    {
        return Metrics::value()
            ->count(User::query())
            ->name('New users')
            ->range(Period::Today)
            ->withChangeAgainstPreviousPeriod();
    }
}
```

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/metrics-for-laravel.md)
