indexzer0/eloquent-filtering
Filter Laravel Eloquent models using simple arrays and request data—no custom query spaghetti. Define allowed filters on your models, support complex search, and keep queries readable, maintainable, and easy to extend for APIs and dashboards.
This package provides the ability for you to create two different types of custom filters. - Field Filter. - Custom Filter.
'custom_filters' => [
YourCustomFilter::class,
],
php artisan make:eloquent-filter LowerCaseFilter --type=field
class LowerCaseFilter implements FilterMethod, Targetable
{
use FieldFilter;
public function __construct(
protected string $value,
) {
}
/*
* The unique identifier of the filter.
*/
public static function type(): string
{
return '$lowercase';
}
/*
* The format that the filter data must adhere to.
* Defined as laravel validator rules.
* On fail: throws MalformedFilterFormatException.
*/
public static function format(): array
{
return [
'value' => ['required', 'string'],
];
}
/*
* Apply the filter logic.
*/
public function apply(Builder $query): Builder
{
$target = $this->eloquentContext->qualifyColumn($this->target);
return $query->where(
DB::raw("LOWER({$target})"),
strtolower($this->value)
);
}
}
public function allowedFilters(): AllowedFilterList
{
return Filter::only(
Filter::field('name', ['$lowercase']),
);
}
php artisan make:eloquent-filter AdminFilter --type=custom
class AdminFilter implements FilterMethod
{
use CustomFilter;
/*
* The unique identifier of the filter.
*/
public static function type(): string
{
return '$admin';
}
/*
* Apply the filter logic.
*/
public function apply(Builder $query): Builder
{
return $query->where(
$this->eloquentContext()->qualifyColumn('admin'),
true
);
}
}
public function allowedFilters(): AllowedFilterList
{
return Filter::only(
Filter::custom('$admin'),
);
}
To specify validation messages and attributes along with the rules, you may return a IndexZer0\EloquentFiltering\Filter\Validation\ValidatorProvider from the ::format() method.
public static function format(): ValidatorProvider
{
return ValidatorProvider::from([
'value' => ['required', 'string'],
], [
'string' => 'The :attribute must be a string.',
], [
'value' => 'value attribute',
]);
}
Adding modifiers to your custom filters is achieved by:
IndexZer0\EloquentFiltering\Filter\Contracts\FilterMethod\ModifiableIndexZer0\EloquentFiltering\Filter\Traits\FilterMethod\Composables\HasModifierssupportedModifiers() method on the filter class.$this->hasModifier('modifier_name') in the apply() implementation.class YourFilterWithModifiers implements
FilterMethod,
Targetable,
Modifiable
{
use FieldFilter;
use HasModifiers;
//...
public static function supportedModifiers(): array
{
return ['special'];
}
public function apply(Builder $query): Builder
{
if ($this->hasModifier('special')) {
// Perform your special logic.
} else {
// Perform your regular logic.
}
}
//...
}
All FilterMethod classes have access to an EloquentContext object that allows you to qualifyColumn of the target.
Benefits of using this method:
->pivot() allowed field filters.public function apply(Builder $query): Builder
{
return $query->where(
$this->eloquentContext()->qualifyColumn($this->target),
true
);
}
How can I help you explore Laravel packages today?