Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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.