koenhoeijmakers / laravel-filterable
Explicit request filtering.
Installs: 73 493
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=8.1
- laravel/framework: ^10.0
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
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()); } }