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.
Request handling for filter[], sort, include). MIT-licensed and battle-tested (4.5K stars) reduces risk./users?filter[name]=John&sort=-created_at).?filter[status]=active&filter[created_at][gt]=2023-01-01).?fields[users]=id,name&include=posts).QueryBuilder::for($existingQuery)).filter[] arrays).sort to only id/created_at).spatie/laravel-query-builder’s non-Eloquent extensions.Query Builder or a dedicated ORM like October CMS’s BackendList.spatie/laravel-query-builder via GraphQL resolvers or use nwidart/laravel-eloquent-filters."This package standardizes how our APIs handle filtering, sorting, and data inclusion—like adding ‘autocomplete’ for query parameters. For example, instead of manually parsing ?name=John&sort=-created_at in every endpoint, we’ll use a single, secure library to handle it. This reduces dev time by ~30% for new APIs, cuts bugs from inconsistent query logic, and makes our public APIs more predictable for partners. It’s like upgrading from hand-written SQL to a modern ORM—just for API queries."
ROI:
?sort=password).*"This replaces repetitive Request parsing with a fluent, type-safe API. Key wins:
filter[], sort, include syntax (e.g., /users?filter[status]=active&include=posts).allowedSorts(['id', 'created_at'])).QueryBuilder::for(User::where('active', true)).AllowedFilter::custom() or AllowedSort::custom().with() for includes).Migration Path:
/products).QueryBuilder::for($existingQuery) to wrap legacy logic.Request handlers.Example Migration:
// Before (manual parsing)
public function index(Request $request) {
$query = User::query();
if ($request->has('name')) {
$query->where('name', 'like', '%'.$request->name.'%');
}
return $query->get();
}
// After (standardized)
public function index() {
return QueryBuilder::for(User::class)
->allowedFilters('name') // Only allow `?filter[name]=...`
->get();
}
```"
**Dependencies**: Requires Laravel 8+; works with Lumen. No breaking changes in v7.x.
How can I help you explore Laravel packages today?