https://github.com/mjedari/larafilter
This package is a simple object-oriented query string filter for Laravel applications. you can create a filter class -by command and make your query filtering logic clean and strongly maintainable.
https://github.com/mjedari/larafilter
filter laravel query-string
Last synced: about 1 month ago
JSON representation
This package is a simple object-oriented query string filter for Laravel applications. you can create a filter class -by command and make your query filtering logic clean and strongly maintainable.
- Host: GitHub
- URL: https://github.com/mjedari/larafilter
- Owner: mjedari
- License: mit
- Created: 2020-12-16T14:50:59.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-27T17:23:23.000Z (about 4 years ago)
- Last Synced: 2025-07-30T02:02:39.814Z (6 months ago)
- Topics: filter, laravel, query-string
- Language: PHP
- Homepage:
- Size: 190 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README

# Laravel Query String Filter
[](https://packagist.org/packages/mjedari/larafilter)
[](https://scrutinizer-ci.com/g/mjedari/larafilter/build-status/master)
[](https://scrutinizer-ci.com/g/mjedari/larafilter/?branch=master)
[](https://scrutinizer-ci.com/g/mjedari/larafilter)


[](https://packagist.org/packages/mjedari/larafilter)
This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.
# Quick start
### Create a filter
```bash
php artisan make:filter country
```
### Implement your filter logic
In the filter class which is created, implement your login in the ``apply()`` method.
In order to get query value just use ``$this->value``. We retrieved it for you from your request.
```php
...
public function apply(Builder $builder)
{
return $builder->where('country', $this->value);
}
...
```
### Register filter class for model
Before registering you should use ``Filterable`` trait in your model.
```php
use Filterable;
...
...
protected static $filters = [
Country::class,
];
```
### Use it!
```php
// All registered filtered are available through this method:
User::filter()->get();
// Only Specific registered filter is available and executable:
User::filterThrough([Country::class])->get();
```
***
# Installation
You can install the package via composer:
```bash
composer require mjedari/larafilter
```
Then you can publish config file:
```bash
php artisan vendor:publish --provider "Mjedari\Larafilter\LarafilterServiceProvider"
```
# Usage
## Initiation
Its simple.First create a filter by this command:
```bash
php artisan make:filter filter-name"
```
Command will create a class under the default directory ``App\Filters`` :
```php
namespace App\Filters;
use Illuminate\Database\Eloquent\Builder;
use Mjedari\Larafilter\Filters\FilterContract;
class Active extends FilterContract
{
public function apply(Builder $builder)
{
// TODO: Implement apply() method.
}
public function options()
{
// TODO: Implement options() method.
}
/*
* Set rules on the query string
*/
public function rules()
{
return [
//
];
}
}
```
Your filter logic would be implemented in the ``apply()`` method:
```php
public function apply(Builder $builder)
{
return $builder->where('avtive', $this->value);
}
```
The important thing is that you have access query string value by ``$this->value`` in your filter class.
## Using
For Which model you want to filter you should add ``Filterable`` trait in it.
```php
class User extends Authenticatable
{
use Filterable;
.
.
.
```
Then add related filters that you created. It should be static property:
```php
use App\Filters\Active;
use App\Filters\City;
class User extends Authenticatable
{
use Filterable;
protected static $filters = [
Active::class,
City::class
];
.
.
.
```
Every thing is ready. just use it in your queries:
```php
User::filter()->get();
```
if you want to specify some filter you can pass them thought this method:
```php
User::filterThrough([City::class])->get();
```
It's good to mention that this package works with query string. Ex:
```php
Route::get('/?country=germany', function() {
return User->filter()->get();
});
```
So you should pass params through the url. The default query name is filter class name. Of course you can change the filters query name by:
```php
class CountryFilter extends FilterContract
{
public static $queryName = 'country';
.
.
.
```
Also, you can set rules on your query string parameters:
```php
class Active extends FilterContract
{
public function rules()
{
return [
'required',
Rule::in(['true', 'false']),
];
}
```
More than that sometimes we would like cast query string value. So:
```php
class Active extends FilterContract
{
protected $cast = 'boolean';
public function rules()
{
return [
//
];
}
```
### Testing
``` bash
composer test
```
### Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
### Security
If you discover any security related issues, please email i.jedari@gmail.com instead of using the issue tracker.
## Credits
- [Mahdi Jedari](https://github.com/mjedari)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.