Number formatting
formatUsing() takes a Closure(float): string and adds a formatted key to the result without changing the raw numbers — a chart plots the value, a label prints the string:
use Illuminate\Support\Number;
Metrics::value()
->sum(Order::query(), 'total')
->formatUsing(fn (float $value): string => Number::currency($value, 'USD'))
->toArray();
// result.value => 1500.0, result.formatted => "$1,500.00"Trends and partitions
For trends and partitions the formatter runs on every point, and formatted becomes a map with the same keys:
use Illuminate\Support\Number;
Metrics::trend()
->sum(Order::query(), 'total')
->daily()
->range('MTD')
->formatUsing(fn (float $value): string => Number::abbreviate($value))
->toArray();
// result.formatted => ['2024-06-01' => '12K', '2024-06-02' => '9K', ...]
Metrics::partition()
->count(User::query(), 'plan')
->formatUsing(fn (float $value): string => number_format($value).' users')
->toArray();
// result.formatted => ['pro' => '120 users', 'free' => '80 users']- Value and Progress — value is formatted into a single formatted string.
- Trend — each trends bucket is formatted into a formatted map.
- Partition — each partitions value is formatted into a formatted map.
prefix() and suffix() are separate, lighter presentation hints: they travel in the envelope untouched, for your front-end to place around the number. Laravel’s Number helpers require the intl PHP extension.
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 cryptoBy 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.