zihad / pilter
A filter package to help developers to attach filter queries to their query builder.
Requires
- php: ^8.0
- laravel/framework: >=9.0
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- spatie/laravel-ray: ^1.35
This package is auto-updated.
Last update: 2025-09-23 05:27:30 UTC
README
Pilter is a Laravel package designed to simplify and organize filter and sorting queries in your Laravel applications. By separating these operations into dedicated classes, Pilter helps make your code cleaner, more readable, and easier to maintain.
Installation
You can install the package via Composer:
composer require zihad/pilter
Usage/Examples
Create a new Filter
class PostFilter
php artisan make:filter Post
namespace App\Filters; use Zihad\Pilter\Filters\Filter; class PostFilter extends Filter { // }
You can use the Filter class like this:
$posts = (new PostFilter(Post::query(), ['title' => 'Post 1', 'sort' => '-title'])) ->filter() ->sort() ->get();
Or
namespace App\Models; use Zihad\Pilter\Traits\Filterable; // Use Filterable trait in model class Post extends Model { use Filterable; } $posts = Post::query() ->applyQuery(['title' => 'Post 1', 'sort' => '-title']), PostFilter::class) ->get();
You can filter and sort by defining separate methods in PostFilter.php
// Filter method protected function title($title) { $this->getQuery()->where('title', $title); } // Sort method protected function sortTitle($order) { $this->getQuery()->orderBy('title', $order); }
Or you can do the basic filters and sorting by adding then in filterable and sortable attributes in PostFilter.php
protected array $filterableFields = ['title', 'category', 'slug']; protected array $sortableFields = ['title', 'category', 'slug'];