Weave 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 Model Filter Laravel Package

lacodix/laravel-model-filter

A Laravel package for filtering, searching, and sorting Eloquent models using query strings. It includes built-in filter types for strings, dates, numbers, and enums, plus custom filters for complex database logic.

View on GitHub
Deep Wiki
Context7

laravel-model-filter adds powerful, reusable filtering, searching, and sorting to Laravel Eloquent models. Define filters with Artisan (make:filter) for strings, dates, numbers, enums, or custom database logic, then apply them directly to models or through relations and nested relations using the RunsOnRelation trait.

  • Query-string support for filters, search, and sort
  • Built-in string, date, number, and enum filter types
  • Custom filters for complex database logic
  • Relation and nested-relation filtering with RunsOnRelation
  • Filter visualisation and generated filter classes
Frequently asked questions about Laravel Model Filter
How do I install lacodix/laravel-model-filter in a Laravel app?
Install via Composer with composer require lacodix/laravel-model-filter. Then generate filters using php artisan make:filter with options such as --type=date --field=created_at. Add the HasFilters trait to your Eloquent model and declare the filters you want to use. See the documentation site for full setup and configuration details.
Which Laravel versions does laravel-model-filter support?
The technical evaluation confirms compatibility with Laravel 10+ and notes no version-specific changes in v4.5.1. The package depends on core Laravel components such as Eloquent, Blade, and the Query Builder. Pin the package version in composer.json and review the upgrade guide before future minor or major updates.
How do I filter Eloquent models by query string parameters?
Use the filterByQueryString() method to apply filters from request parameters and searchByQueryString() for search terms. These methods are designed to be RESTful and API-friendly, reducing friction between frontend and backend. You can also combine query-string filtering with sorting and search modes in your controller.
Can I filter models through relationships and nested relations?
Yes. The RunsOnRelation trait lets filters run inside a whereHas closure and automatically qualifies field names with the related table. This supports filtering on relations and nested relations, such as applying an AuthorFilter through Post::whereHas('author'). Use it when you need to constrain parent models by related model attributes.
What built-in filter types are available in laravel-model-filter?
The package includes built-in filter types for strings, dates, numbers, and enums. It also provides filter modes such as GREATER_OR_EQUAL and search modes like STARTS_WITH and CONTAINS_ANY. For logic beyond these types, you can create custom filter classes.
How do I create a custom complex filter for specific database logic?
Generate a filter with php artisan make:filter and implement your specific query logic in the filter class. Custom filters can handle complex database logic that built-in types do not cover. For advanced cases like multi-table joins or raw SQL, expect to write manual query code and test it carefully.
Is laravel-model-filter safe against query injection?
The package dynamically binds fields and values, so treat user input as untrusted and validate it before applying filters. Whitelist filterable fields and avoid passing arbitrary column names from request data into filter arrays. The technical evaluation specifically flags query injection risk as unchanged and recommends input validation or whitelisted fields.
How does laravel-model-filter perform on large datasets?
Dynamic query building can introduce N+1 queries and unbounded LIKE searches, so performance work is still your responsibility. Use eager loading, database indexes, and search modes like STARTS_WITH where appropriate. For large datasets above 100K records, test with DB::enableQueryLog() and consider Scout or Algolia for full-text search and aggregations.
Can I use laravel-model-filter for APIs or does it require Blade?
The filterByQueryString() and searchByQueryString() methods are suitable for APIs and server-side filtering in Laravel applications. The filter visualization feature is tied to Blade components, so headless APIs and non-Laravel frontends cannot use that UI layer directly. You can still expose filtering, searching, and sorting through JSON endpoints.
How should I test filters and relation filtering in my Laravel application?
Write feature tests that create models with factories, apply filters, and assert the returned results. Prioritize regression tests for query-string parsing and relation filtering with whereHas closures. You can instantiate filter classes directly and apply them to query builder closures in your test cases.
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.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections