lacodix / laravel-model-filter
A Laravel package to filter, search and sort models with ease while fetching from database.
Fund package maintenance!
lacodix
Installs: 24 441
Dependents: 0
Suggesters: 0
Security: 0
Stars: 169
Watchers: 3
Forks: 12
Open Issues: 1
Type:laravel-package
Requires
- php: ^8.1
- ext-intl: *
- illuminate/contracts: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- illuminate/database: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- laravel/pint: ^1.0
- laravel/prompts: ^0.1.16
- nunomaduro/collision: ^7.0|^8.0
- nunomaduro/larastan: ^2.0.1
- nunomaduro/phpinsights: ^2.6
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.21
- pestphp/pest-plugin-faker: ^2.0
- pestphp/pest-plugin-laravel: ^2.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^10.0
- rector/rector: ^1.0
- spatie/pest-plugin-test-time: ^2.0
- dev-master
- 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/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: 2024-10-23 16:27:31 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.
Once installed you can filter, search and sort Models. You can create own filters based on our base filters, create individual filters, or use one of the extended filters, that are ready to use with less or even without configuration like TrashedFilter.
Additionally you can use the visualisation functionality of filters.
Documentation
You can find the entire documentation for this package on our documentation site
Installation
composer require lacodix/laravel-model-filter
Basic Usage
Filter
Create your first filter
php artisan make:filter CreatedAfterFilter --type=date --field=created_at
// 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.