### Install Query Builder for Laravel via Composer Source: https://github.com/roundly-consulting/query-builder-for-laravel/blob/main/README.md Install the package using Composer. Requires PHP 8.4 and Laravel 12 or 13. ```bash composer require roundly-consulting/query-builder-for-laravel ``` -------------------------------- ### Define allowed filters and sorts in a controller Source: https://github.com/roundly-consulting/query-builder-for-laravel/blob/main/README.md Shows how to use QueryBuilder::for() with allowedFilters() and allowedSorts() to permit specific query string parameters. The example includes exact, partial, scope, and trashed filters, plus a default sort. Unpermitted filters or sorts are rejected with a 400. ```php 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()); } ``` -------------------------------- ### Validate per_page with HasPageSize in a form request Source: https://github.com/roundly-consulting/query-builder-for-laravel/blob/main/README.md Shows a form request that uses the HasPageSize concern to validate the per_page parameter. Values above the configured maximum return a 422. ```php use Illuminate\Foundation\Http\FormRequest; use RoundlyConsulting\QueryBuilder\Concerns\HasPageSize; final class ListPostsRequest extends FormRequest { use HasPageSize; public function rules(): array { return $this->pageSizeRules(); } } ```