Skip to content

Foysal50x/query-filter

Repository files navigation

A elegant way to filter database based on query filter.

Latest Version on Packagist Build Status Quality Score Total Downloads

This is a simple package, It's can boost your productivity, make your code cleaner as well as simple.

Installation

You can install the package via composer:

composer require faisal50x/query-filter

Usage

Before

 public function index($request){
    $query = User::query();
     if(request()->has('status')) {
        $query = $query->where('status', request()->get('status'));
     }
     if(request()->has('role')) {
        $query = $query->where('role', request()->get('role'));
     }
    $users = $query->get();
}

Now

public function index($request, UserFilter $filter){
    //It's nice and clean
    $users = User::filter($filter)->get();
}

// User Filter
use Faisal50x\QueryFilter\QueryFilter;

class UserFilter extends QueryFilter {

    public function status($query, $status){
        return $query->whereStatus($status);
    }
    
    public function role($query, $role){
        return $query->whereRole($role);
    }
    
}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.