Skip to content

Latest commit

 

History

History
46 lines (37 loc) · 1.65 KB

README.md

File metadata and controls

46 lines (37 loc) · 1.65 KB

Laravel Filterable

Build Packagist Packagist license

A Laravel package to implement filtering by request parameters.

example.com/json?name=Koen&sortBy=name&desc=0

Usage

Require the package.

composer require koenhoeijmakers/laravel-filterable

Inject it in your controller (or resolve it from the container in any other way).

namespace App\Http\Controllers\Api\User;

use KoenHoeijmakers\LaravelFilterable\Contracts\Filtering;

final class Index
{
    public function __construct(
        private readonly Filtering $filtering
    ) {}

    public function __invoke()
    {
        $builder = User::query();
        
        $this->filtering->builder($builder)
            ->filterFor('name', fn(Builder $builder, string $value) => $builder
                ->where('name', 'like', "{$value}%");
            )
            ->sortFor('name')
            ->defaultSorting('name')
            ->filter();
    
        return UserResource::collection($builder->paginate());
    }
}