milebits / eloquent-filters
A Laravel PHP library that allows the user to filter the result of Eloquent Model query builders.
This package's canonical repository appears to be gone and the package has been frozen as a result. Email us for help if needed.
Installs: 124
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 2
Open Issues: 1
pkg:composer/milebits/eloquent-filters
Requires
- php: >=8.0
- laravel/framework: ^6|^7|^8|^9
- milebits/helpers: ^3
Requires (Dev)
- facade/ignition: 2.*
- fakerphp/faker: 1.*
- mockery/mockery: 1.*
- nunomaduro/collision: 5.*
- phpunit/phpunit: 9.*
README
A Laravel PHP library that allows the user to filter the result of Eloquent Model query builders.
How to install
1 - Install via composer
composer require milebits/eloquent-filters
2 - Add the trait to the filterable models
Inside the model you want to make it filterable you might want to add the Milebits\Eloquent\Filters\Concerns\Filterable trait to your model so.
Should be looking like this:
use Illuminate\Database\Eloquent\Model; use Milebits\Eloquent\Filters\Concerns\Filterable; class Post extends Model { use Filterable; }
How to use
Creating new filters
To create new filters you need to use the make from artisan using
bash php artisan make:filter MyFiler
Which will generate a new filter file inside the app/Filters folder, or you can use this which will do the same
bash php artisan make:filter My
Or if you would like to make this file fresh, you will have to use it like this
namespace App\Filters; use Illuminate\Database\Eloquent\Builder; use Milebits\Eloquent\Filters\Filters\ModelFilter; /** * Class FilterName * @package App\Filters */ class FilterName extends ModelFilter { /** * The filter to apply to the model builder from the pipeline * * @param Builder $builder * @return Builder */ public function apply(Builder $builder): Builder { return $builder->where($this->key(), '=', $this->keyValue()); } }
How to filter data
Use the filter
In order to filter data you can use the filtered function that will be inside you model
function index(){ return App\Models\Post::filtered()->paginate(); }
Adding filters to the filter
By adding them to the function
function index(){ return App\Models\Post::filtered([NameFilter::class, EnableFilter::class])->paginate(); }
By using default filter array
Inside your model you can add an array of filters named $filters and this property must be public.
use Milebits\Eloquent\Filters\Filters\NameFilter; use Milebits\Eloquent\Filters\Filters\EnableFilter; use Illuminate\Database\Eloquent\Model; use Milebits\Eloquent\Filters\Concerns\Filterable; class Post extends Model{ use Filterable; public static array $filters = [ NameFilter::class, EnableFilter::class, ]; }
By doing this you can now use the filtered function without any parameters just like before.
function index(){ return App\Models\Post::filtered()->paginate(); }
The Scopes
This package give you access to a great deal of scopes with useful common fields, in order not to waist time coding these scopes over and over.
- Scope
Milebits\ELoquent\Filters\ActivationCodeFieldgives you access to:activation_codecolumn. - Scope
Milebits\ELoquent\Filters\ActiveFieldgives you access to:activecolumn. - Scope
Milebits\ELoquent\Filters\AmountFieldgives you access to:amountcolumn. - Scope
Milebits\ELoquent\Filters\ContentFieldgives you access to:contentcolumn. - Scope
Milebits\ELoquent\Filters\DescriptionFieldgives you access to:descriptioncolumn. - Scope
Milebits\ELoquent\Filters\EmailFieldgives you access to:emailcolumn. - Scope
Milebits\ELoquent\Filters\EnableFieldgives you access to:enabledcolumn. - Scope
Milebits\ELoquent\Filters\NameFieldgives you access to:namecolumn. - Scope
Milebits\ELoquent\Filters\PasswordFieldgives you access to:passwordcolumn. - Scope
Milebits\ELoquent\Filters\PhoneFieldgives you access to:phonecolumn and also tophone_verified_at. - Scope
Milebits\ELoquent\Filters\PriceFieldgives you access to:pricecolumn. - Scope
Milebits\ELoquent\Filters\SlugFieldgives you access to:slugcolumn. - Scope
Milebits\ELoquent\Filters\TitleFieldgives you access to:titlecolumn. - Scope
Milebits\ELoquent\Filters\UsernameFieldgives you access to:usernamecolumn.
The Filters
This package gives you access to a great deal of filters which you can use with their respective field scopes.
- Filter
Milebits\Eloquent\Filters\Filters\ActiveFiltergives you access to:activecolumn filter. - Filter
Milebits\Eloquent\Filters\Filters\AmountFiltergives you access to:amountcolumn filter. - Filter
Milebits\Eloquent\Filters\Filters\EnableFiltergives you access to:enablecolumn filter. - Filter
Milebits\Eloquent\Filters\Filters\NameFiltergives you access to:namecolumn filter. - Filter
Milebits\Eloquent\Filters\Filters\PhoneFiltergives you access to:phonecolumn filter. - Filter
Milebits\Eloquent\Filters\Filters\PhoneVerifiedFiltergives you access to:phone_verified_atcolumn filter. - Filter
Milebits\Eloquent\Filters\Filters\PriceFiltergives you access to:pricecolumn filter. - Filter
Milebits\Eloquent\Filters\Filters\SlugFiltergives you access to:slugcolumn filter. - Filter
Milebits\Eloquent\Filters\Filters\SortFiltergives you access to:sortcolumn filter. - Filter
Milebits\Eloquent\Filters\Filters\TitleFiltergives you access to:titlecolumn filter.
These scopes and filters are rich with eloquent scopes methods, and intelligent methods to use, I cannot possibly document them all, but you can easily find them in the scopes or filters themselves.
Contributions
If in any case while using this package, and you which to request a new functionality to it, please contact us at suggestions@os.milebits.com and mention the package you are willing to contribute, or suggest a new functionality.
Vulnerabilities
If in any case while using this package, you encounter security issues or security vulnerabilities, please do report them as soon as possible by issuing an issue here in GitHub or by sending an email to security@os.milebits.com with the mention Vulnerability Report milebits/eloquent-filters as your subject.