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
Eloquent Filtering

Eloquent Filtering Laravel Package

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+.

View on GitHub
Deep Wiki
Context7

Powerful eloquent filtering

Frequently asked questions about Eloquent Filtering
How do I implement basic filtering for an Eloquent model in Laravel 10?
Extend your model with the `Filterable` trait and define allowed filters in the `allowedFilters()` method using `Filter::only()`. For example, `Filter::field('name', [FilterType::EQUAL])` enables exact name matching. Then call `Model::filter($request->filters)` to apply the filters from an HTTP request array.
Does this package support complex filter combinations like `$or` and `$and`?
Yes, the package supports nested conditions using arrays. For example, pass `[['$or', [['target' => 'price', 'type' => '$gt', 'value' => 100], ['target' => 'name', 'type' => '$eq', 'value' => 'TV']]]]` to create OR conditions. This works seamlessly with Eloquent’s query builder.
Will this package work with Laravel Scout for full-text search?
Yes, you can combine Eloquent Filtering with Scout. Use the package for structured filtering (e.g., price ranges, categories) and Scout for full-text search (e.g., fuzzy name matching). Chain them like `Model::filter($request->filters)->scoutSearch($query)` for hybrid search.
What Laravel and PHP versions are supported?
The package requires **Laravel 10+** and **PHP 8.2+**. It is not compatible with older Laravel versions (e.g., 9.x) or PHP 8.1 or below. Check the [GitHub repo](https://github.com/IndexZer0/eloquent-filtering) for breaking change notes if upgrading.
How do I handle JSON fields in filters (e.g., filtering nested JSON data in PostgreSQL)?
Use the `$jsonContains` or `$jsonLength` filter types for JSON fields. For example, `Filter::field('metadata', [FilterType::JSON_CONTAINS])` will filter PostgreSQL JSON columns. Note that MySQL/SQLite may require additional configuration or fallbacks for JSON support.
Is there a performance impact when using complex filters in production?
The package generates dynamic SQL, which can produce verbose queries for deeply nested `$or`/`$and` conditions. Benchmark with your dataset, but avoid overusing nested filters in high-traffic APIs. Use eager loading (`with()`) to mitigate N+1 issues if filtering relationships.
Can I use this package with Laravel Nova or Voyager for admin panels?
Yes, Eloquent Filtering integrates smoothly with Nova/Voyager. Define filters in your model, then pass the request filters to the query builder. For Nova, override the `resolveQuery()` method in a resource to apply `Model::filter($request->filters)`.
How do I secure filters against SQL injection if user input is involved?
The package sanitizes inputs by validating against `allowedFilters()`. Always restrict filter targets to model attributes/relationships (e.g., `Filter::field('price', [FilterType::GT])`). For untrusted input, validate/sanitize values before passing them to `filter()`.
What alternatives exist for filtering Eloquent models in Laravel?
Alternatives include **custom query scopes** (for simple cases), **Laravel Scout** (for full-text search), or **API packages like Spatie’s Laravel Query Builder**. Eloquent Filtering stands out for its declarative syntax, support for complex conditions, and seamless HTTP request integration.
How do I test filter combinations in my Laravel application?
Write unit tests by mocking the `filter()` method and asserting query constraints. For integration tests, use `Http::fake()` to simulate filtered requests and verify results. Test edge cases like empty filters, invalid types, and nested conditions. The package includes unit tests as a reference.
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.
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
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope