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
Model Filter

Model Filter Laravel Package

laraditz/model-filter

Flexible Eloquent model filtering for Laravel with operator support (eq, gte, etc.), AND/OR grouping, and relationship filters via dot notation. Add a Filterable trait and allowlist fields, optionally create filter classes for custom logic, and filter using request params under filters[].

View on GitHub
Deep Wiki
Context7

A simple eloquent model filter.

Frequently asked questions about Model Filter
How do I enable filtering on a Laravel Eloquent model?
Add the `Filterable` trait to your model and define a `$filterable` array with allowed fields (e.g., `['name', 'role.name']`). Relationships use dot notation. No extra configuration is needed for basic filtering.
What’s the difference between v1 and v2 of this package?
v2 requires filter parameters to be namespaced under `filters[]` (e.g., `?filters[name]=John`). v1 used flat keys like `?name=John`. If upgrading, update your API routes and client requests to match the new format.
Can I use custom operators like `LIKE` or `IN` with this package?
Yes, but only for fields without custom filter methods. The package auto-handles operators like `eq`, `gte`, `lte`, `neq`, and `in`. For `LIKE` or other custom logic, define a custom filter method and handle the operator manually.
How do I filter nested relationships (e.g., `user.role.department`)?
Use dot notation in the `$filterable` array (e.g., `['role.department']`). The package will automatically traverse the relationship chain and apply the filter to the nested attribute.
Does this package support OR conditions between filters?
No, all filters are combined with AND logic by default. For OR conditions, you’ll need to manually chain queries or use Laravel’s `orWhere` after applying the filter.
How do I validate filter inputs before applying them?
The package doesn’t include built-in validation. Use Laravel’s `Validator` or `FormRequest` to validate the `filters[]` input before passing it to `Model::filter()`. Example: `Validator::validate($request->all(), ['filters.*' => 'sometimes|string'])`.
Will this package work with Laravel 8 or older versions?
No, it requires **PHP 8.1+** and **Laravel 9–13**. If you’re on an older version, consider alternatives like `spatie/laravel-query-builder` or manually implementing filtering logic.
How do I optimize performance for deep relationship filters?
Use Laravel’s `with()` or `load()` to eager-load relationships before filtering. Example: `User::with('role.department')->filter($request->all())`. Without eager loading, deeply nested filters risk N+1 query issues.
Can I restrict filter access (e.g., only admins can filter `sensitive_field`)?
Yes, implement middleware or gates to validate user permissions before calling `Model::filter()`. Example: `if (!auth()->user()->isAdmin()) { unset($request->filters['sensitive_field']); }`
What are some alternatives to this package for Laravel filtering?
Consider `spatie/laravel-query-builder` (more flexible but complex), `beberlei/doctrine-extensions` (for Doctrine users), or `laravel-scout` (for search-specific needs). This package is ideal if you want a lightweight, Eloquent-native solution.
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.
sayedenam/sayed-dashboard
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