andrewdyer/filtering

1.0.1 2023-01-07 21:25 UTC

This package is auto-updated.

Last update: 2024-11-08 01:13:11 UTC


README

A super tidy and easy to extend solution for filtering Laravel database results with a query string.

Total Downloads Latest Stable Version License

License

Licensed under MIT. Totally free for private or commercial projects.

Installation

composer require andrewdyer/filtering

Basic Usage

$courses = Course::filter($request->getQueryParams())->get();

Creating Filters

namespace App\Filters;

use Anddye\Filtering\Filter;
use Illuminate\Database\Eloquent\Builder;

class AccessFilter extends Filter
{
    public function filter(Builder $builder, $value): Builder
    {
        return $builder->where('free', $value);
    }
}

Creating Filterers

namespace App\Filterers;

use Anddye\Filtering\Filterer;
use App\Filters\AccessFilter;

class CourseFilterer extends Filterer
{
    protected array $filters = [
        'access' => AccessFilter::class,
    ];
}

Create Filter Scope

namespace App\Models;

use App\Filterers\CourseFilterer;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    public function scopeFilter(Builder $builder, array $queryParams): Builder
    {
        return (new CourseFilterer())->filter($builder, $queryParams);
    }
}

Support

If you're using this package, I'd love to hear your thoughts! Feel free to contact me on Twitter.

Found a bug? Please report it using the issue tracker, or better yet, fork the repository and submit a pull request.