Download the PHP package pricecurrent/laravel-eloquent-filters without Composer
On this page you can find all versions of the php package pricecurrent/laravel-eloquent-filters. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pricecurrent/laravel-eloquent-filters
More information about pricecurrent/laravel-eloquent-filters
Files in pricecurrent/laravel-eloquent-filters
Package laravel-eloquent-filters
Short Description Advanced Laravel models filtering capabilities
License MIT
Homepage https://github.com/pricecurrent/laravel-eloquent-filters
Informations about the package laravel-eloquent-filters
Advanced Laravel models filtering capabilities
Installation
You can install the package via composer:
Usage
This package gives you fine-grained control over how you may go about filtering your Eloquent Models.
This package is particularly good when you need to address complex use-cases, implementing filtering on many parameters, using complex logic.
But let's start with simple example:
Consider you have a Product, and you need to filter products by name
:
Generate eloquent-filter
This will put your Filter to the app/Filters directory by default.
You may prefix the name with the path, like Models/Product/NameFilter
.
You can use the --field=name
argument to generate your filter with the field name
Here is what your NameFilter
might look like:
Notice how our Filter has no clue it is tied up with a specific Eloquent Model? That means, we can simply re-use it for any other model, where we need to bring in the same name filtering functionality:
You can chain methods from the filter as if it was simply an Eloquent Builder method:
To enable filtering capabilities on an Eloquent Model simply import the trait Filterable
More complex use-case
This approach scales very well when you are dealing with a real-life larger applications where querying data from the DB goes far beyond simple comparison by a name field.
Consider an app where we have Stores with a Location coordinates and we have products in stock and we need to query all products that are in stock in a store that is in 10 miles radius
We may stuff all the logic in the controller with some pseudo-code:
This breaks Open-Closed principle and makes the code harder to test and maintain. Adding new functionality becomes a disaster.
Much Better!
We can now distribute the filtering logic to a dedicated class
Now we have no problem with testing our functionality
And controller can be just tested with mocks or stubs, just making sure we have called the necessary filters.
Checking filtering is applicable
Each filter provides method isApplicable()
which you might implement and return boolean. If false
is returned, the apply
method won't be called.
This is helpful when we don't control the incoming parameters to the filter class. In the example above we can do something like this:
Of course you may take another approach where you are in control of what's being passed into the filter parameters, instead of just blindly passing in the request payload. You could leverage DTO and type-hinting for that and have Filters collection factories to properly build a collection of filters. For instance
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Andrew Malinnikov
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-eloquent-filters with dependencies
illuminate/database Version ^8.0|^9.0|^10.0|^11.0|^12.0
illuminate/support Version ^8.0|^9.0|^10.0|^11.0|^12.0
spatie/laravel-package-tools Version ^1.9