NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages

Open source

Metrics for Laravel

Install
composer require roundly-consulting/metrics-for-laravel
Requires: PHP ^8.4 · Laravel ^12.0|^13.0

Overview

A native metrics engine for Laravel. Point it at any Eloquent query and get a dashboard-ready metric back — a single value with period-over-period change, a time series for charts, progress against a target, or a grouped breakdown for pie and bar charts. It handles date ranges, aggregation, time bucketing, caching and number formatting, and returns clean JSON-friendly data for any front-end — Nova-style metrics without Nova. No migrations, no tables, no UI; MIT-licensed and built on official Laravel components plus two small Roundly helper packages.

What you get

Four metric types

Value, trend, progress and partition — the four shapes behind nearly every dashboard tile, inline or as reusable classes.

Ranges & comparison

20 preset periods, custom windows and per-metric timezones, with change against the previous period or any range you pick.

Chart-ready trends

Minute-to-year buckets, gap-filled axes and multi-series splits, portable across MySQL, MariaDB, PostgreSQL and SQLite.

Readable partitions

Top-N breakdowns with a rolled-up “Other” bucket and display labels mapped from raw group keys.

Registry & dashboards

Register metrics under keys, resolve a whole dashboard in one call, and return either straight from a controller as JSON.

Caching & events

Opt-in result caching with targeted invalidation on any store, and a MetricCalculated event for slow-metric logging.

Facade or DI, with a recording fake

Use the Metrics facade or inject MetricsManager for the same API; Metrics::fake() stubs results and records what was resolved.

Documentation

Installation

Install via Composer — the provider and the Metrics facade are auto-discovered, there are no migrations, and the config is optional.

Configuration

Every config key and its default — reporting timezone, default range, unit and precision, result caching, the partition label and trend drivers.

Quick start

Build a metric inline in one fluent expression with the Metrics facade, then read its typed result or return its JSON-ready envelope.

The Metrics facade

The whole API on one facade — inline builders, the metric registry, dashboards, cache invalidation and the recording fake.

Without the facade

Inject MetricsManager for the same API without static calls, or use metric classes on their own — no registry, no facade.

Metric classes

Define reusable Value, Trend, Progress and Partition classes with calculate() and setup(), plus the fluent API every metric shares.

Aggregate helpers

count, average, sum, max and min over any Eloquent query — the aggregated column, the date column, and how scopes are respected.

Date ranges

20 preset periods from today to last year, custom windows, per-metric timezones and the Period enum toolkit for pickers and validation.

Period comparison

Show change against the previous period, or compare against any range with compareTo() — for value and progress metrics.

Trends

Time series bucketed by minute to year, with gap filling, multi-series groupBy() and date grouping portable across four databases.

Partitions

Grouped breakdowns for pie and bar charts — largest first, capped to the top N with an “Other” bucket, and mapped to display labels.

Output & results

The envelope every metric returns, the result shape of each metric type, and the typed result() objects with their accessors.

Number formatting

Present values as currency, percentages or abbreviated figures with formatUsing() — the raw numbers stay untouched alongside.

Registry

Register metrics under short keys as classes, closures or instances, then resolve one or all of them by name from anywhere.

Dashboards

Resolve many registered metrics in one call into a single keyed payload that shares one range and, optionally, one timezone.

Caching

Opt-in result caching — globally or per metric with cache(), cacheFor(), cacheKey() and dontCache() — plus forget() and flushCache().

Events

MetricCalculated fires on every resolution with the key, range, duration and cache-hit flag — ready for slow-metric logging.

Artisan commands

List every registered metric and resolve one from the terminal to inspect its envelope, with an optional range override.

Extending

Add trend support for another database with a QueryExpression, and feed metrics from any package’s domain events.

Exceptions

Every exception extends MetricsException — what each one means, when it is thrown, and how to catch the whole family at once.

Testing

Stub metric results with Metrics::fake(), assert which metrics were resolved or forgotten, and test the numbers against seeded rows.

Requirements

PHP 8.4+ and Laravel 12 or 13, with MySQL, MariaDB, PostgreSQL or SQLite for trend metrics out of the box.

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

By donating, you agree to our donation terms.

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.