indexzer0/eloquent-filtering
Define allowed filters on your Eloquent models and apply them from simple arrays or request data—no custom query logic. Supports complex, type-based filtering for APIs and dashboards on Laravel 10+ / PHP 8.2+.
By default, when specifying an Filter::relation() or Filter::morphRelation(), fields within that relationship are not included in the allowed filter list.
You can specify allowed filters inside a relation in two ways.
includeRelationFields()Use ->includeRelationFields() on Filter::relation() or Filter::morphRelation().
public function allowedFilters(): AllowedFilterList
{
return Filter::only(
Filter::relation('manufacturer', [FilterType::HAS])
->includeRelationFields()
);
}
For Filter::morphRelation(), you should specify the models for which to include the relation fields for.
public function allowedFilters(): AllowedFilterList
{
return Filter::only(
Filter::morphRelation(
'subscribable',
[FilterType::HAS_MORPH],
)->includeRelationFields([
FoodDeliveryService::class,
Saas::class,
])
);
}
allowedFiltersAlternatively, if you don't want to use ->includeRelationFields(), you can define allowedFilters for each Filter::relation() and Filter::morphType().
public function allowedFilters(): AllowedFilterList
{
return Filter::only(
Filter::relation(
target: 'manufacturer',
types: [FilterType::HAS],
allowedFilters: Filter::only(
Filter::field('name', [FilterType::LIKE])
)
)
);
}
public function allowedFilters(): AllowedFilterList
{
return Filter::only(
Filter::morphRelation('subscribable', [FilterType::HAS_MORPH],
Filter::morphType(
type: FoodDeliveryService::class,
allowedFilters: Filter::only(
Filter::field('name', [FilterType::EQUAL])
)
),
Filter::morphType(
type: Saas::class,
allowedFilters: Filter::only(
Filter::field('name', [FilterType::EQUAL])
)
),
)
);
}
How can I help you explore Laravel packages today?