https://github.com/gurgentil/eloquent-filters
Add query filters to your Eloquent models with ease.
https://github.com/gurgentil/eloquent-filters
eloquent filters laravel
Last synced: about 2 months ago
JSON representation
Add query filters to your Eloquent models with ease.
- Host: GitHub
- URL: https://github.com/gurgentil/eloquent-filters
- Owner: gurgentil
- License: mit
- Created: 2020-11-09T19:38:51.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-01-15T22:12:57.000Z (over 4 years ago)
- Last Synced: 2025-01-02T15:24:14.703Z (4 months ago)
- Topics: eloquent, filters, laravel
- Language: PHP
- Homepage:
- Size: 35.2 KB
- Stars: 17
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Eloquent Filters
[](https://github.com/gurgentil/eloquent-filters/releases)

[](https://scrutinizer-ci.com/g/gurgentil/eloquent-filters)
[](LICENSE.md)This package provides a nice and easy way to add query filters to your Eloquent models without turning your model
classes into massive God objects.After setting it up, if you are building an API, for example, you can simply add this piece of code to your controller:
```php
User::filter($request->get('filter'))->get();
```Now make a request to `/api/users?filter[name]=John` and let the fun begin.
## Installation
Install the package via composer:
```bash
composer require gurgentil/eloquent-filters
```## Usage
Create a filter builder that extends `Gurgentil\EloquentFilters\FilterBuilder`. This is where you will register all the filters for your Eloquent model.
```php
namespace App\Filters;use Gurgentil\EloquentFilters\FilterBuilder;
class UserFilters extends FilterBuilder
{
protected $availableFilters = [];
}
```Add the `Gurgentil\EloquentFilters\Filterable` trait to your model class.
The trait will look for a filter builder for the model inside `App\Filters`.## Creating filters
Run the artisan command:
```bash
php artisan make:filter Users/NameFilter
```Register the filter in the builder you created previously:
```php
namespace App\Filters;use App\Filters\Users\NameFilter;
use Gurgentil\EloquentFilters\FilterBuilder;class UserFilters extends FilterBuilder
{
protected $availableFilters = [
'name' => NameFilter::class,
];
}
```Last but not least, implement your query in `NameFilter`:
```php
namespace App\Filters\Users;use Gurgentil\EloquentFilters\Filter;
use Illuminate\Database\Eloquent\Builder;class NameFilter implements Filter
{
/**
* Apply filter.
*
* @return Builder
*/
public function apply(Builder $builder, $value)
{
return $builder->where('name', $value);
}
}
```You can perform filter queries on your model by passing a list of filters to the `filter` method:
```php
User::filter([
'name' => 'John',
])->get();
```## Testing
``` bash
composer test
```## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Credits
- [Gustavo Rorato Gentil](https://github.com/gurgentil)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.