roundly-consulting / query-builder-for-laravel
Native, allow-list-driven filter, sort and pagination for Laravel API list endpoints
Package info
github.com/roundly-consulting/query-builder-for-laravel
pkg:composer/roundly-consulting/query-builder-for-laravel
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/contracts: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/http: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- roundly-consulting/enums-for-laravel: ^1.0
- roundly-consulting/package-toolkit-for-laravel: ^1.0
- symfony/http-kernel: ^7.2|^8.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- roundly-consulting/testing-for-laravel: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Query Builder for Laravel
Allow-list-driven filtering, sorting and pagination for Laravel API list endpoints. It reads the
query string (filter[...], sort=, page, per_page) and applies only the filters and sorts a
controller explicitly permits — anything else is rejected with a 400.
Installation
Requires PHP 8.4, Laravel 12 or 13.
composer require roundly-consulting/query-builder-for-laravel
Usage
Declare what a list endpoint may filter and sort, then use it like any Eloquent builder:
use RoundlyConsulting\QueryBuilder\AllowedFilter; use RoundlyConsulting\QueryBuilder\AllowedSort; use RoundlyConsulting\QueryBuilder\QueryBuilder; // GET /posts?filter[status]=published,draft&filter[title]=laravel&sort=-popularity&per_page=50 public function index(ListPostsRequest $request) { return QueryBuilder::for(Post::class) ->allowedFilters( AllowedFilter::exact('status'), // a comma list becomes whereIn AllowedFilter::partial('title'), // escaped LIKE, ILIKE on Postgres AllowedFilter::scope('published', booleans: true), AllowedFilter::trashed(), ) ->allowedSorts('title', AllowedSort::field('popularity', 'views')) ->defaultSort('-created_at') ->paginate($request->perPage()); }
HasPageSize validates per_page on the form request — above the configured maximum is a 422:
use Illuminate\Foundation\Http\FormRequest; use RoundlyConsulting\QueryBuilder\Concerns\HasPageSize; final class ListPostsRequest extends FormRequest { use HasPageSize; public function rules(): array { return $this->pageSizeRules(); } }
Documentation
The full documentation — configuration, every feature and its API, and testing — lives on our website: roundly-consulting.com/open-source/docs/query-builder-for-laravel
Release notes are in CHANGELOG.md. To contribute, see the contributing guide.
Support our work
This package is free and open source, built and maintained by Roundly Consulting. If it saves you time, please consider supporting our open-source work — a one-time donation, a monthly pledge on Patreon or a crypto donation helps fund maintenance, new features and new packages.
License
The MIT License (MIT). Please see LICENSE.md. Maintained by roundly-consulting.