Laravel Pipeline Query Collection
Published on by Paul Redmond
The Laravel Pipeline Query Collection package contains a collection of Eloquent query filters for pipelines. Given a complex query of filters, the code can get a bit unwieldy around query conditions:
$users = User::query() ->when($request->name ?? null, function($query, $name) { $query->where('name', 'like', "%$name%"); }) ->when($request->is_admin ?? null, function($query, $isAdmin) { $query->where('is_admin', $isAdmin ? 1 : 0); }) ->when($request->created_at_from ?? null, function($query, $date) { $query->where('created_at', '>=', $date); }) ->when($request->created_at_to ?? null, function($query, $date) { $query->where('created_at', '<=', $date); }) ->get();
Using this package could be written as follows:
use Baro\PipelineQueryCollection; // users?name=Baro&is_admin=1&created_at_from=2022-06-01&created_at_to=2022-06-31$users = Users::query()->filter([ new PipelineQueryCollection\RelativeFilter('name'), new PipelineQueryCollection\BooleanFilter('is_admin'), new PipelineQueryCollection\DateFromFilter('created_at'), new PipelineQueryCollection\DateToFilter('created_at'),])->get();
At the time of writing, this package contains the following filters:
- Bitwise
- Boolean
- Date from
- Date to
- Exact
- Relation
- Relative
- Scope
- Sort
You can get started with this package on GitHub at l3aro/pipeline-query-collection. The package's author also wrote a more in-depth article—Building a sexy query filter—which walks you through the idea behind the package.