spatie/laravel-query-builder
Build safe, flexible Eloquent queries from incoming API requests. Supports whitelisted filtering (partial/exact/scope/custom), sorting, includes, field selection, pagination, and grouped AND/OR filters—ideal for JSON:API-style endpoints with minimal boilerplate.
API Flexibility & Developer Experience:
?filter[name]=John, ?sort=-created_at) across all endpoints.Roadmap Priorities:
Request handling for filter[], sort, include) and leverage a battle-tested, MIT-licensed solution.tenant_id via ?filter[tenant_id]=123.?sort=-revenue).include behavior for nested relationships (e.g., ?include=posts.comments.author).Use Cases:
status and date_range).?fields[users]=id,name,email to reduce bandwidth.?filter[active]=true&filter[plan]=pro).Adopt if:
Request parsing.AllowedFilter constraints).filter[q] for OR/AND groups).Look elsewhere if:
FULLTEXT searches).nuwave/lighthouse instead)."This package lets our API endpoints adapt dynamically to user needs—like a self-service data layer. For example, a sales dashboard can filter orders by status=shipped and date_range=2024-01-01..2024-01-31 without backend changes. It’s like giving frontend teams a ‘query builder’ for our data, reducing API versioning and speeding up feature delivery. The MIT license and 4.5K stars mean it’s battle-tested and low-risk."
*"Spatie’s Query Builder solves the pain of manually parsing filter[], sort, and include params in every API route. Key benefits:
?filter[active]=true).QueryBuilder::for(User::class)
->allowedFilters(
AllowedFilter::partial('name'),
AllowedFilter::groupOr('q', [
AllowedFilter::partial('email'),
AllowedFilter::exact('role', 'admin'),
]),
)
->allowedSorts('created_at')
->allowedIncludes('posts.comments');
Trade-offs: We cede some control to a third-party package, but the trade-off is worth it for the time saved and reduced bugs in query parsing."*
How can I help you explore Laravel packages today?