lacodix / laravel-model-filter
A Laravel package to filter, search and sort models with ease while fetching from database.
Package info
github.com/lacodix/laravel-model-filter
Type:laravel-package
pkg:composer/lacodix/laravel-model-filter
Fund package maintenance!
Requires
- php: ^8.2
- ext-intl: *
- illuminate/contracts: ^11.0|^12.0|^13.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- illuminate/database: ^11.0|^12.0|^13.0
- illuminate/support: ^11.0|^12.0|^13.0
- larastan/larastan: ^2.0.1|^3.1
- laravel/pint: ^1.0
- laravel/prompts: ^0.1|^0.3
- nunomaduro/collision: ^7.0|^8.0
- nunomaduro/phpinsights: ^2.6
- orchestra/testbench: ^9.0|^10.0|^11.0
- pestphp/pest: ^3.0|^4.0
- pestphp/pest-plugin-faker: ^3.0|^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- rector/rector: ^2.0
- spatie/pest-plugin-test-time: ^2.2
- dev-master
- v4.3.0
- v4.2.0
- v4.1.0
- v4.0.2
- v4.0.1
- v4.0.0
- v3.8.3
- v3.8.2
- v3.8.1
- v3.8.0
- v3.7.0
- v3.6.2
- v3.6.1
- v3.6.0
- v3.5.2
- v3.5.1
- v3.5.0
- v3.4.0
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.x-dev
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v1.x-dev
- v1.11.3
- v1.11.2
- v1.11.1
- v1.11.0
- v1.10.0
- v1.9.4
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- v1.8.5
- v1.8.4
- v1.8.3
- v1.8.2
- v1.8.0
- v1.7.0
- v1.6.1
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- v0.8.1
- v0.8.0
- v0.7.5
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.0
- dev-feat/not-contains
- dev-feat/testing
- dev-feat/filter-group-fallback
- dev-fix/request-get-deprecation
- dev-chore/boost-fix
- dev-feat/boost
- dev-fix/eager-loading-on-nested-search
- dev-fix/requirements
- dev-fix/numeric-filter-view
- dev-fix/sqlite-date-filter
- dev-feat/laravel-12
- dev-feat/prepopulation-cache-options
- dev-chore/queryname
- dev-fix/searchquery
- dev-feat/enum-on-the-fly
- dev-feat/nested-search
- dev-style/stan
- dev-docs/update-docs
- dev-renky-patch-1
- dev-fix/belongs-to-filter
- dev-doc-v3-fix
- dev-v3-doc-update
This package is auto-updated.
Last update: 2026-03-23 12:22:33 UTC
README
This package allows you to filter, search and sort models while fetching from database with ease. It contains additional functionality to use query strings to filter, search and sort.
With this package you can easily filter, search and sort your Eloquent models. It supports various filter types like strings, dates, numbers, and enums out of the box. You can also create complex custom filters to handle any specific database logic.
Additionally you can use the visualisation functionality of filters.
Documentation
You can find the entire documentation for this package on our documentation site
See our Upgrade Guide for information on how to upgrade from older versions.
Installation
composer require lacodix/laravel-model-filter
Basic Usage
Filter
Create your first filter
php artisan make:filter CreatedAfterFilter --type=date --field=created_at
Filters can be applied directly to models, but they can also be easily applied to relations and nested relations
using the RunsOnRelation trait. This automatically wraps the filter logic in a whereHas closure and correctly
qualifies the field names using the related table.
// Set the filter mode // App\Models\Filters\CreatedAfterFilter public FilterMode $mode = FilterMode::GREATER_OR_EQUAL; // Apply this filter and the HasFilters trait to a Model // App\Models\Post use HasFilters; protected array $filters = [ CreatedAfterFilter::class, ]; // Somwhere in a controller, select all posts created after 1st of January 2023 Post::filter(['created_after_filter' => '2023-01-01'])->get(); // Do the same via query string by calling // this url: https://.../posts?created_after_filter=2023-01-01 Post::filterByQueryString()->get();
Search
// add searchable fields and the IsSearchable trait to Model: // App\Models\Post use IsSearchable; protected array $searchable = [ 'title', 'content', ]; // Somewhere in controller, find all posts that contain "test" in title or content Post::search('test')->get(); // Do the same via query string by calling // this url: https://.../posts?search=test Post::searchByQueryString()->get();
Visualize
All filters have a blade template that can visualize the filter with one or multiple input fields. To visualize all filters of a dedicated model you can use a blade component:
<x-lacodix-filter::model-filters :model="Post::class" />
Grouping
Sometimes you don't need all of the filters for all parts of a web application. Maybe there shall be different filters be available to the backend as in the frontend, or different user types shall be able to use different filters.
For such cases this package offers filter grouping when adding filters to models
protected array $filters = [ 'frontend' => [ HotFilter::class, ], 'backend' => [ CreatedAfterFilter::class, PublishedFilter::class, ] ];
The groups can be used in the scopes
Post::filterByQueryString('frontend')->get()
or
Post::filter(['hot_filter' => 'hot'], 'frontend')->get(); Post::filter(['created_after_filter' => '2023-01-01'], 'backend')->get();
Testing
composer test
Contributing
Please run the following commands and solve potential problems before committing and think about adding tests for new functionality.
composer rector:test composer insights composer csfixer:test composer phpstan:test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.