---
title: "Registry — Metrics for Laravel | Roundly"
description: "Register metrics under short keys as classes, closures or instances, then resolve one or all of them by name from anywhere."
url: https://roundly-consulting.com/open-source/docs/metrics-for-laravel/registry
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)

# Registry

Register your dashboard’s metrics under short keys — usually in a service provider’s boot() — and resolve them by name. A metric can be a class-string (resolved through the container), a closure returning a metric, or a ready instance:

```php
use App\Models\Order;
use RoundlyConsulting\Metrics\Facades\Metrics;

// in a service provider boot()
Metrics::register('registered_users', RegisteredUsers::class);                 // class-string
Metrics::register('revenue', MonthlyRevenueGoal::class);
Metrics::register('users_by_plan', fn () => UsersByPlan::make());              // closure
Metrics::register('orders_today', fn () => Metrics::value()->count(Order::query())->range('TODAY'));
Metrics::register('signups_today', RegisteredUsers::make()->range('TODAY'));   // instance
```

## Resolving

```php
Metrics::has('revenue');          // true
Metrics::keys();                  // ['registered_users', 'revenue', ...] — without resolving
Metrics::get('revenue');          // a new MonthlyRevenueGoal, tagged with its key
Metrics::get('revenue')->key();   // 'revenue'
Metrics::all();                   // ['registered_users' => RegisteredUsers, ...] — all resolved
```

| Method | Returns | Notes |
| --- | --- | --- |
| `register(string $key, string\|Metric\|Closure $metric)` | `MetricsManager` | Chainable. |
| `unregister(string $key)` | `MetricsManager` | Chainable; a no-op for an unknown key. |
| `get(string $key)` | `Metric` | Resolves a fresh copy and tags it with its key; throws UnknownMetricException if unregistered. |
| `has(string $key)` | `bool` | Whether the key is registered. |
| `keys()` | `list<string>` | Registered keys, without resolving them. |
| `all()` | `array<string, Metric>` | Resolves every registered metric. |
| `dashboard(array $keys)` | `Dashboard` | Batch resolution — see Dashboards. |

get() tags the metric with its key — key() returns it, and the MetricCalculated event carries it. An unregistered key throws RoundlyConsulting\\Metrics\\Exceptions\\UnknownMetricException. Every get() returns a new metric — a registered instance is copied — so a range() or timezone() applied to what it returns never leaks into the next call (or, under Octane, the next request).

## Serving metrics from a controller

```php
// in a controller
return response()->json(
    collect(Metrics::all())->map->toArray()
);

// or a single metric by key, applying the request's period
return response()->json(
    Metrics::get($key)->range($request->input('period', 'TODAY'))->toArray()
);
```

Because every metric is Responsable, the controller can also return it directly — authorization stays your app’s responsibility:

```php
public function show(string $key)
{
    return Metrics::get($key)->range(request('period', 'TODAY'));
}
```

## 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)
