App-owned gap-filled daily trends in Laravel PHP
The subset to inline
Replace one chart's daily UTC count with an authorized aggregate query plus explicit zero filling. Keep the response envelope and underlying data untouched. Arbitrary aggregates/columns, weekly or hourly intervals, local-time DST reporting, and a general analytics query builder are out of scope. If callers use those features, inventory and retain them rather than silently shrinking their API. Verify it in your app with the trial checklist below.
What the package does
Trend::query accepts an existing Eloquent builder; aggregate converts it to the base query, adds inclusive whereBetween, groups by a driver-specific date label, and merges real values with Carbon period zero placeholders. Model/global/tenant scopes are therefore important inputs, not optional filters to drop.
At the source baseline, PostgreSQL hour labels include seconds while the Carbon hour placeholder label does not. PostgreSQL ISO-week SQL and Carbon calendar-year week labels can disagree at year boundaries; the MySQL/SQLite week formats differ again. tests/ExampleTest.php only asserts true. Do not use that test or a naive date-format translation as evidence of parity. This recipe explicitly chooses daily UTC labels and a half-open time window.
Endpoint-owned implementation
- Capture the existing chart's model scopes, filters, selected date column/type, time zone, aggregate, timestamp bounds, number types, and output envelope. Document the half-open replacement contract: first displayed UTC date inclusive, midnight after the last displayed date exclusive. Translate existing inclusive-end callers, not just their parameter names.
- Validate date-only inputs and a bounded day count. Use immutable UTC dates for the application-side labels. Reject reversed, malformed, and too-large ranges; do not let a query parameter select a SQL identifier, raw aggregate, format string, or timezone expression.
- Begin with the existing authorized Eloquent relationship/query, including deletion/visibility predicates. Aggregate once in SQL; never load an unbounded row set into PHP and never query separately for every day. Apply bound
>= startand< endExclusiveconstraints to the raw timestamp column. - Hardcode the expression appropriate to the deployed driver and actual column. For PostgreSQL
timestamptz, a daily expression isto_char(created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD'). A UTC-valued timestamp without timezone needs no conversion. For MySQLTIMESTAMP, enforce/test the session timezone; SQLite requires a verified UTC storage format. Do not pretend one expression is portable. - Use
selectRawonly for app-owned fixed SQL, e.g. the verified day expression andCOUNT(*) AS aggregate, followed by fixedgroupBy/orderBylabels. A fresh raw-table query must recreate every applicable Eloquent scope; prefer preserving the scoped builder. Check result labels are unique and within the requested window, and normalize counts to integers after range checks. - Initialize expected UTC labels with zero, replace only those keys with aggregate counts, then return an ordered array of
{date, aggregate}points in the original envelope. Avoid PHP numeric-key or collection merge rules changing the transport shape. Empty data still produces all requested buckets. Do not reuse this zero rule for nullable averages without a separate contract. - If cached, key by tenant, filters, bounds, time zone and aggregate. Never share a tenant chart through a global key. Database schema changes or date-column conversions are rollout decisions outside this extraction.
Filling fragment only; $start/$endExclusive are validated CarbonImmutable UTC-midnight values, $counts contains checked integer values by exact day label, and query/authorization remain the caller's responsibility:
$points = [];
for ($day = $start; $day->lt($endExclusive); $day = $day->addDay()) {
$label = $day->format('Y-m-d');
$points[] = ['date' => $label, 'aggregate' => $counts[$label] ?? 0];
}
Do not replace immutable dates with a mutable cursor that accidentally changes the bound used for the query.
Trial checklist
Use three UTC days beginning 2026-01-30: two authorized rows on the first, none on the second, one exactly at the third midnight, plus another tenant's row and a row at the exclusive end. Expect three ascending unique labels and counts [2, 0, 1]. Include a soft-deleted row to verify the actual scope policy.
- Cover empty/one-day windows, reversed input, an excessive range, leap day, and month/year boundaries.
- Change the database session timezone away from UTC; timestamps around midnight still group as declared.
- Compare SQL and Carbon labels byte for byte, and ensure a real count replaces a zero rather than appending a duplicate.
- Verify numeric count serialization, original chart envelope, tenant cache isolation, and a bounded query count.
- Compare the source-backed path on matching fixtures, recording any deliberate endpoint-boundary change before removing
Trendcalls.
Run application chart feature tests plus integration coverage on the production driver. Only after used intervals/aggregates have replacements should composer why flowframe/laravel-trend cease showing callers. Record versions and executed results with the change.
Sources
- MIT reference: flowframe/laravel-trend v0.5.0,
src/Trend.php, all SQL adapters,TrendValue.php, andtests/ExampleTest.php. PreserveLICENSE.mdwhen adapting code. - Laravel PHP framework baseline. Use the application's actual Laravel/Carbon/database versions, not an assumed latest API.