Generate easily metrics and trends data of your models for your dashboards.
composer require eliseekn/laravel-metrics
- MySQL support
- Verbose query builder
- Custom columns and table definition
- Days and months translation with Carbon
Import the Eliseekn\LaravelMetrics\LaravelMetrics
class in your controller and use it as follows :
- Basic usage
// generate trends of products amount's sum for the current year
LaravelMetrics::query(Product::query())
->count()
->byMonth()
->trends();
// generate trends of orders amount's sum for the last 6 months of the current year including current month
LaravelMetrics::query(Order::query())
->sum('amount')
->byMonth(6)
->trends();
// generate total orders amount's sum
LaravelMetrics::query(Order::query())
->sum('amount')
->byYear()
->metrics();
// generate total product count for the current day
LaravelMetrics::query(Product::query())
->count()
->byDay(1)
->metrics();
- Using custom query
LaravelMetrics::query(
Post::query()->where('user_id', auth()->id())
)
->count()
->byDay()
->trends();
- Using custom date column
LaravelMetrics::query(Post::query())
->count()
->byDay()
->dateColumn('published_at')
->trends();
- Using date range
LaravelMetrics::query(Post::query()))
->count()
->between('2020-05-01', '2022-08-21')
->trends();
- Using custom label column
LaravelMetrics::query(Order::query())
->count()
->byMonth(12)
->labelColumn('status')
->trends();
- Using custom table
LaravelMetrics::query(
Order::query()->join('users', 'orders.id', 'users.order_id')
)
->count()
->table('users')
->labelColumn('name')
->trends();
LaravelMetrics::query(
DB::table('orders')
)
->sum('amount')
->byMonth()
->trends();
Add HasMetrics
trait to your models and use it as follows :
Order::metrics()
->sum('amount')
->byMonth()
->trends();
LaravelMetrics::query(...)
->byDay(int $count = 0) // or
->byWeek(int $count = 0) // or
->byMonth(int $count = 0) // or
->byYear(int $count = 0) // or
->between(string $startDate, string $endDate, string $dateIsoFormat)
Note : Periods are defined for the current day, week, month or year by default. However, you can define a specific value using dedicated methods. For example:
// generate trends of orders count for the year 2023
LaravelMetrics::query(Order::query())
->count()
->byMonth(12)
->forYear(now()->year)
->labelColumn('status')
->trends();
// generate total orders amount's sum for the third month only
LaravelMetrics::query(Product::query())
->sum('amount')
->byMonth(1)
->forMonth(3)
->metrics();
LaravelMetrics::query(...)
->forDay(int $day)
->forWeek(int $week)
->forMonth(int $month)
->forYear(int $year)
Note : Make sure to employ the 'fillEmptyDates' method when utilizing the 'between' period to automatically populate any missing dates with a default value. For example:
LaravelMetrics::query(...)
->count()
->between(Carbon::now()->subDays(10)->format('Y-m-d'), Carbon::now()->format('Y-m-d'))
->fillEmptyDates()
->trends();
LaravelMetrics::query(...)
->count(string $column = 'id') // or
->average(string $column) // or
->sum(string $column) // or
->max(string $column) // or
->min(string $column)
LaravelMetrics::query(...)
->trends() // or
->metrics()
Combining different time periods and data aggregates can enhance your overall experience. For example :
LaravelMetrics::query(...)
->sumByMonth()
->trends();
LaravelMetrics::query(...)
->counyByMonth(count: 12)
->forYear(now()->year)
->labelColumn('status')
->trends();
LaravelMetrics::query(...)
->countBetween(Carbon::now()->subDays(10)->format('Y-m-d'), Carbon::now()->format('Y-m-d'))
->fillEmptyDates()
->trends();
...
Days and months names are automatically translated using config(app.locale)
except 'week' period.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected]
instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
You can find a demo project here.
This package was generated using the Laravel Package Boilerplate.