roundly-consulting / metrics-for-laravel
Calculate value, trend, progress, and partition metrics from any Eloquent query.
Package info
github.com/roundly-consulting/metrics-for-laravel
pkg:composer/roundly-consulting/metrics-for-laravel
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/contracts: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- roundly-consulting/enums-for-laravel: ^1.0
- roundly-consulting/package-toolkit-for-laravel: ^1.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- nunomaduro/collision: ^8.5
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- roundly-consulting/testing-for-laravel: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Metrics for Laravel
Turn any Eloquent query into a ready-to-render dashboard metric: a single value with period-over-period change, a time series for charts, progress against a target, or a partitioned breakdown. Date ranges, timezones, aggregation, caching and formatting are handled for you, and the result comes back as clean JSON for any front-end — no admin panel required.
Installation
Requires PHP 8.4 and Laravel 12 or 13.
composer require roundly-consulting/metrics-for-laravel
Usage
Build a metric inline from any query — toArray() is the JSON-ready envelope:
use App\Models\Order; use App\Models\User; use RoundlyConsulting\Metrics\Enums\Period; use RoundlyConsulting\Metrics\Facades\Metrics; Metrics::value()->count(User::query()) ->range(Period::Today) ->withChangeAgainstPreviousPeriod() ->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')->limit(5)->toArray();
Or read the typed result in PHP:
$revenue = Metrics::value()->sum(Order::query(), 'total')->range(Period::ThisMonth)->result(); $revenue->value(); // 15230.0
Register metrics under keys and return a whole dashboard from one endpoint:
Metrics::register('signups', fn () => Metrics::value()->name('Sign-ups')->count(User::query())); Metrics::register('revenue', fn () => Metrics::progress()->name('Revenue')->sum(Order::query(), 'total')->target(10000)); return Metrics::dashboard(['signups', 'revenue'])->range(Period::ThisMonth); // Responsable: JSON
Documentation
The full documentation — configuration, every feature and its API, and testing — lives on our website: roundly-consulting.com/open-source/docs/metrics-for-laravel
Release notes are in CHANGELOG.md. To contribute, see the contributing guide.
Support our work
This package is free and open source, built and maintained by Roundly Consulting. If it saves you time, please consider supporting our open-source work — a one-time donation, a monthly pledge on Patreon or a crypto donation helps fund maintenance, new features and new packages.
License
The MIT License (MIT). Please see License File for more information.