rgalstyan/laravel-aggregated-queries
Laravel Aggregated Queries helps you combine multiple database queries into one aggregated call, reducing round trips and simplifying complex reporting. It keeps query builder workflows familiar while making data fetching more efficient and code easier to maintain for dashboards and reports.
json_agg / JSON_ARRAYAGG / json_group_array). That is a scoped, reversible engineering task rather than a new Redis cluster or read replica.Consider it when:
belongsTo, hasOne, hasMany) and a query count you actually monitor (Telescope/Debugbar, DB::listen).select() narrowing, withCount instead of loading, and Laravel's native chaperone() for hasMany chains.Look elsewhere when:
->each->save(), accessors/casts/$appends on children, nested eager loads inside the aggregated relation, or whereHas on already-loaded relations. Aggregation returns decoded JSON; verify hydration, casting, and global-scope/soft-delete behavior in a spike before assuming parity.chaperone() already gets you most of the win at zero dependency cost.To executives: We have a specific class of slow endpoints where the database round trips, not the database work, drive latency. This package is a small, MIT-licensed utility that lets one request fetch a parent and its children in a single query instead of six. It is a time-boxed experiment with a measurable outcome — query count and p95 on one endpoint — and no infrastructure spend. It is not a platform commitment; it is reversible and license-permissive enough that we can fork it if the maintainer walks away. Recommendation: fund a one-sprint spike, decide on data.
To engineering: Drop-in optimization for belongsTo/hasOne/hasMany eager loads that swaps Eloquent's per-relation IN (...) queries for a single JSON-aggregate statement, with the dialect-specific SQL handled for you. Costs and caveats: unproven dependency (10 stars, 0 dependents), so pin the version and isolate it behind a query object so we can swap to native chaperone(), hand-rolled SQL, or a read model later. Before adoption we need a benchmark on a real endpoint, contract tests asserting the JSON shape and model hydration (casts, accessors, soft deletes, global scopes), and a feature flag for rollback.
How can I help you explore Laravel packages today?