Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Model Filter Laravel Package

lacodix/laravel-model-filter

Filter, search, and sort Eloquent models with reusable filter classes and query-string support. Includes built-in types (string, date, number, enum), relation/nested relation filtering, custom complex logic, and filter visualisation.

View on GitHub
Deep Wiki
Context7

title: Filter for Relations weight: 4

In the simple cases, you can use the RunsOnRelation trait to run any existing filter on a relation.

However, if you have more complex requirements, like nested relations, you can manually use Laravel's whereHas method or use the RunsOnRelation trait with a dot-notated relation name.

Basic Relation Filter

For most cases, just using the trait is enough.

class PostTitleFilter extends StringFilter
{
    use RunsOnRelation;

    protected string $relation = 'post';
    protected string $field = 'title';
}

Nested Relations

You can also filter through nested relations by using a dot-notated relation name:

class AuthorPostTitleFilter extends StringFilter
{
    use RunsOnRelation;

    protected string $relation = 'author.posts';
    protected string $field = 'title';
}

This will automatically wrap the logic in whereHas('author.posts', ...) and correctly qualify the title column using the posts table name.

Complex Custom Relation Filters

If you need more control, such as adding additional constraints to the whereHas query, you can manually override the applyFilter method while still using the trait:

class CustomRelationFilter extends StringFilter
{
    use RunsOnRelation;

    protected string $relation = 'posts';
    protected string $field = 'title';

    public function applyFilter(Builder $query): Builder
    {
        // Add custom constraints inside the whereHas closure
        $query->where('published', true);

        // Call parent logic to apply the string filter on the title field
        return parent::applyFilter($query);
    }
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours