tucker-eric/eloquentfilter ↗
Created Feb 16, 2021 · View the tucker-eric/eloquentfilter repository page
An Eloquent Way To Filter Laravel Models And Their Relationships
Want this for your repo?
Render a free sample of any GitHub repo in seconds.
Contributors
15
Lines of Code
285
From
Mar 4, 2016
To
Oct 20, 2020
About tucker-eric/eloquentfilter
Eloquent Filter is a PHP package for Laravel that simplifies filtering Eloquent models and their relationships through a structured, convention-based approach. Instead of writing complex nested conditional queries to filter models by multiple parameters, developers can define filter logic in dedicated filter classes that automatically map query parameters to filter methods using a camel-cased naming convention.
The package allows developers to implement the `Filterable` trait on any Eloquent model, which enables calling a `filter()` method that processes query input and applies constraints. Filter classes follow a predictable naming pattern (like `UserFilter` for the `User` model) and use intuitive method definitions where the method name corresponds to the query parameter. The package automatically handles common cases like ignoring empty values, dropping `_id` suffixes from parameters, and providing helper methods for common SQL patterns like LIKE queries with wildcards.
A notable feature is the built-in support for filtering by related models through two approaches: the `$relations` array for declaratively mapping related model filters, and the `related()` method for defining local filtering logic on relationships. Both methods use `whereHas()` under the hood to nest relationship constraints efficiently. The package also includes pagination helpers, dynamic filter selection, blacklist/whitelist method support for controlling which methods execute, and setup hooks that run before any filters are applied. Optional service provider registration enables artisan commands for generating filter classes.