spatie/laravel-query-builder

API development

Builds Eloquent queries from API request parameters: filter, sort, include relations and select fields, using only the options you allow.

The problem it solves

Letting clients filter or sort by arbitrary columns is unsafe; writing the whitelist by hand for each endpoint is repetitive.

Install

composer require spatie/laravel-query-builder

Example

From the package README: /users?filter[name]=John&include=posts&sort=id.

use Spatie\QueryBuilder\QueryBuilder;

$users = QueryBuilder::for(User::class)
    ->allowedFilters('name')
    ->allowedIncludes('posts')
    ->allowedSorts('id')
    ->get();

Requirements and activity

Fetched from Packagist and GitHub on 2026-11-29. Constraints show what the package allows, not what its maintainers tested.

Latest stable7.3.9 (2026-11-13)
PHP^8.4
Laravel^12.0|^13.0
LicenseMIT
Downloads (Packagist, total)40,115,756
GitHub stars4,468
Last push2026-11-13
StatusNot marked abandoned

Strengths

  • Only whitelisted filters, includes and sorts are accepted.
  • Starts from any existing Eloquent builder, so your scopes still apply.

Limits to know

  • Request parameter shapes follow the package conventions (filter[...], include, sort, fields[...]); clients must use them.

Official links

Documentation · Packagist · GitHub

Write-up based on: github.com/spatie/laravel-query-builder#readme. Written from the official README and docs; not tested by Laravel Questions. All packages.

Our network