Installation and configuration
Installation
composer require myprogs/query-builderThe service provider is discovered automatically. The package needs PHP 8.3 or newer and Laravel 12 or 13.
Configuration
The defaults work without a config file. Publish it when you need to change parameter names, the delimiter or how unknown parameters are treated:
php artisan vendor:publish --tag=query-builder-configThe file is config/query-builder.php. It is also published under the tag that spatie/laravel-query-builder documents, so existing scripts keep working:
php artisan vendor:publish --tag=laravel-query-builder-configParameter names
'parameters' => [
'include' => 'include',
'filter' => 'filter',
'sort' => 'sort',
'fields' => 'fields',
'append' => 'append',
],The names of the query string parameters. Change them when your API already uses other names, for example 'filter' => 'where' for ?where[name]=john.
Delimiter
'delimiter' => ',',
'filter_value_splitting_enabled' => true,The separator of multi-value parameters: filter[id]=1,2,3, sort=-created_at,name, include=posts,comments. With filter_value_splitting_enabled set to false, filter values are not split and filter[name]=Smith, John is one value. A single filter can use its own delimiter, see Filtering.
Aggregate suffixes
'suffixes' => [
'count' => 'Count',
'exists' => 'Exists',
'min' => 'Min',
'max' => 'Max',
'sum' => 'Sum',
'avg' => 'Avg',
],The suffixes that turn a relation name into an aggregate include: include=postsCount, include=postsExists. See Including relations.
Unknown parameters
'disable_invalid_filter_query_exception' => false,
'disable_invalid_sort_query_exception' => false,
'disable_invalid_include_query_exception' => false,By default a filter, sort or include that the endpoint does not allow throws an exception (a 400 response). Set a key to true to ignore such parameters silently. These keys apply to the spatie-compatible API; the definitions API always reports unknown parameters.
Field names
'convert_relation_names_to_snake_case_plural' => true,
'convert_relation_table_name_strategy' => null,
'convert_field_names_to_snake_case' => false,How fields[...] keys and names are matched:
convert_relation_names_to_snake_case_plural: a relation is keyed by its name in snake case and plural,include=userOwner&fields[user_owners]=id. Withfalse, by the name as written ininclude:fields[userOwner]=id.convert_relation_table_name_strategy: key a relation by the table of the related model instead, converted withsnake_case,camelCaseornone.nullturns it off.convert_field_names_to_snake_case: convert requested field names, sofirstNameselectsfirst_name.
See Selecting fields.
Translations
The error messages of the definitions API are translated into English and Russian and follow the application locale. To change the wording or add a language, publish the translations to lang/vendor/query-builder:
php artisan vendor:publish --tag=query-builder-translationsSee Errors.