Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emyasnikov/laravel-filter
Filtering Eloquent queries based on HTTP requests.
https://github.com/emyasnikov/laravel-filter
Last synced: 11 days ago
JSON representation
Filtering Eloquent queries based on HTTP requests.
- Host: GitHub
- URL: https://github.com/emyasnikov/laravel-filter
- Owner: emyasnikov
- License: mit
- Created: 2022-12-28T09:58:04.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-22T19:44:28.000Z (almost 2 years ago)
- Last Synced: 2024-12-08T00:45:28.400Z (18 days ago)
- Language: PHP
- Homepage: https://packagist.org/packages/intraset/laravel-filter
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Filter
[![Latest Version on Packagist](https://img.shields.io/packagist/v/intraset/laravel-filter.svg?style=flat)](https://packagist.org/packages/intraset/laravel-filter)
[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE.md)
[![Total Downloads](https://img.shields.io/packagist/dt/intraset/laravel-filter.svg?style=flat)](https://packagist.org/packages/intraset/laravel-filter)Filtering Eloquent queries based on HTTP requests.
## Installation
```bash
composer require intraset/laravel-filter
```## Usage
To apply filters, e.g. on the user model, trait has to be used:
```php
orderBy('name');return $builder;
}/**
* Name filter.
*
* @param string|null $value
* @return \Illuminate\Database\Eloquent\Builder
*/
public function name($value = null)
{
$this->builder->where('name', $value);return $this->builder;
}
}
```Filter class can then be used to simplify controllers index handler:
```php
public function index(Request $request, UserFilter $filter)
{
$users = User::filter($filter)->get();return $users;
}
```## Credits
- [Evgenij Myasnikov](https://github.com/emyasnikov)
I got the idea to filter HTTP requests by reading an [article](https://pineco.de/filtering-eloquent-queries-based-on-http-requests/) written by [D. Nagy Gergő](https://github.com/iamgergo) and used the code.
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.