A Laravel package that adds JSON:API-compatible filter and sort query parameter support to Eloquent models.
Features:
- Declarative
$filterableand$sortablearrays on the model — no boilerplate controllers - Full operator set:
eq,ne,gt,gte,lt,lte,like,in,nin,null,not_null,between,not_between,tree - Relation filtering via
whereHassubqueries and pivot column support - Filtered eager-loading with
filter[included.*] - OR / AND condition groups
- Field, relation, and sort resolver interfaces for custom SQL
- Full-result caching (Redis / Memcached) with automatic tag-based invalidation
- Custom field, relation, and sort resolvers for extending filter behaviour
- Tree descendant expansion via the
treeoperator
- PHP 8.2+
- Laravel 11+
composer require jurager/filterableAdd HasFilterable to your model and declare allowed fields:
use Jurager\Filterable\Concerns\HasFilterable;
class Product extends Model
{
use HasFilterable;
protected array $filterable = [
'sku' => ['eq', 'like'],
'price' => ['gte', 'lte', 'between'],
'status' => ['eq', 'in'],
'category.name' => ['eq', 'like'],
'is_active',
];
protected array $sortable = ['id', 'sku', 'price', 'created_at'];
}Apply in the controller:
Product::query()
->filter(request()->query('filter', []))
->sort(request()->query('sort'))
->paginate();Request:
GET /products?filter[status][in][]=active&filter[price][gte]=100&sort=-created_at
To learn more about filtering, sorting, relations, caching, and advanced usage please go to the Documentation.
This package is open-sourced software licensed under the MIT license.