- What does alexmg86/laravel-sub-query do?
- It adds fluent subquery support to Laravel's query builder and Eloquent, letting you embed subqueries in SELECT, FROM, WHERE, JOIN, and ORDER BY clauses through a chainable API. Common uses include aggregating columns from related models, sorting by related data, and limiting how many related rows are considered. It is aimed at developers who want SQL subquery power without hand-writing raw expressions.
- How do I install laravel-sub-query with Composer?
- Install it with Composer using composer require alexmg86/laravel-sub-query. The package integrates with Laravel's query builder and Eloquent, so no manual service provider registration is typically needed for modern Laravel versions. Check the repository README for any version-specific setup notes before rolling it out across a team.
- Which Laravel and PHP versions are supported?
- Support depends on the package release, so confirm the framework and PHP constraints in the package metadata or composer.json before adopting it. Because the v1.9.9 release is additive, it should remain backward compatible for existing users. Verify the supported database drivers as well if you rely on subquery-heavy queries in production.
- How do I sum, min, max, or avg a column on a related model?
- The package lets you select aggregated values from a related table as a subquery column, so you can pull sum, min, max, or avg values alongside your main model without extra queries. This avoids N+1 problems compared to loading relations and aggregating in PHP. It is a good fit for dashboards and list views that need per-row totals such as order amounts or last activity timestamps.
- Can I sort query results by a related model's column?
- Yes, sorting by relations is one of the package's stated use cases. You can order results by a value pulled from a related table via a subquery, which is useful for leaderboards, admin listings, or any table where sorting depends on another model. Test the generated SQL on large tables to make sure the ordering remains performant.
- How do I limit the number of related rows used in a subquery?
- The package supports limiting relation rows inside subqueries, so an aggregate or lookup only considers a bounded set of related records, such as the latest few entries per parent. This keeps subquery results predictable and can reduce work compared to aggregating every related row. Confirm the exact chainable syntax in the README for your installed version.
- What do the new whereCurrentYear, whereCurrentMonth, and whereCurrentDay methods do?
- These helpers, added in v1.9.9, filter records by the current calendar year, month, or day, removing the need to hand-write date-boundary logic in each feature. They are useful for dashboards, admin filters, and scheduled reporting views. Note that release notes do not confirm signature details or timezone handling, so verify whether the boundary uses application time, database time, or UTC before relying on them.
- Will these date helpers produce index-friendly queries on large tables?
- It depends on how the methods translate to SQL. If they generate range predicates on a date column, existing indexes should still be used; if they wrap the column in a database date function, queries may become non-sargable and slow on large tables. Review the generated SQL with EXPLAIN on a representative dataset. Also test month-end, year-end, and leap-day boundaries.
- How should I test laravel-sub-query in my application?
- Add focused tests for the queries you build, covering aggregation correctness, ordering behavior, and relation row limits. For the current-period helpers, include tests around month-end, year-end, leap day, and timezone edge cases, since those are the most likely sources of off-by-one results. Pilot the package in a low-risk reporting or admin filter before using it in high-visibility dashboards or scheduled jobs.
- Is laravel-sub-query well maintained, and what are the alternatives?
- The v1.9.9 release is a low-risk, additive feature update with no indicated breaking changes or migration work, which suggests active incremental development. Maintenance effort for your team should be low, mainly ensuring consistent usage across repositories and query objects rather than ad-hoc date filters scattered through the codebase. Alternatives include writing raw subquery expressions yourself or using other Laravel subquery packages, so compare supported Laravel versions and query features before switching.