Laravel Trend
A fluent Laravel package for aggregating Eloquent models into time-series trends for charts and reports.
Repository Health
Technical Analysis
Laravel Trend is a small, focused package that turns an Eloquent model or query into time-bucketed aggregate data — counts, sums, averages, minimums, and maximums grouped per minute, hour, day, month, or year — for feeding directly into charting libraries or reports, without hand-writing raw SQL date-truncation queries for each database driver.
It is installed as a Laravel service provider and exposes a single fluent Trend class: call Trend::model() or Trend::query(), set a date range with between(), choose an interval (perDay(), perMonth(), etc.), and call an aggregate method to get back an array of TrendValue objects ready to plot.
What You Get
- A fluent Trend::model()/Trend::query() entry point for building time-series aggregations from any Eloquent model or query builder instance
- Interval helpers (perMinute, perHour, perDay, perMonth, perYear) that generate the correct date-truncation SQL per database driver
- Aggregate methods (count, sum, average, min, max) returning a simple array of TrendValue date/value pairs
- Database adapters for MySQL, PostgreSQL, and SQLite so the same query code is portable across environments
- A Laravel Facade (Trend) alongside the underlying class for either usage style
Common Use Cases
- Populating a dashboard chart (via Chart.js, ApexCharts, or similar) with monthly signup or revenue trends without writing custom SQL
- Generating a periodic report of order counts or totals grouped by day or week for an admin panel
- Comparing time-bucketed averages (e.g. average order value per month) across a custom filtered query rather than a whole table
- Building trend widgets in admin frameworks like Filament that need quick chart-ready aggregate data from Eloquent models
Under The Hood
Architecture — The package centers on a single Trend class (~195 lines) that accepts either an Eloquent model class name or a pre-built query builder, applies a date range filter, then delegates database-specific date-truncation SQL generation to one of three small adapter classes (MySqlAdapter, PgsqlAdapter, SqliteAdapter) extending a shared AbstractAdapter contract; the adapter is selected automatically based on the active Laravel database connection driver. Aggregation results are mapped into lightweight TrendValue value objects (date + value) for consumption by chart libraries. Tech Stack — PHP 8.2+, built on Laravel’s illuminate/contracts (supporting Laravel 8 through 13) and spatie/laravel-package-tools for the service-provider boilerplate; tests use Pest with orchestra/testbench to run against a real Laravel application context. Code Quality — The codebase is intentionally tiny (under 300 lines across src/), with each adapter reduced to ~20 lines implementing only the date-truncation SQL fragment for its driver, keeping the diff surface for adding a new database small; a Pest test suite (tests/ExampleTest.php) exercises the public API against an in-memory testbench app. API Design — The two-entry-point design (::model() vs ::query()) cleanly separates “just aggregate this whole model” from “aggregate this already-filtered query,” and the fluent interval/aggregate method chain (perMonth()->average(‘field’)) reads close to natural language, requiring almost no ramp-up for a developer already familiar with Eloquent query builders.