Yazan.
All articles
LaravelPHPBackend

Simplifying Laravel Filtering with the Purity Package

Simplifying Laravel Filtering with the Purity Package

Managing complex filters in Laravel applications can quickly clutter your controllers. The Purity package simplifies this — you keep your code clean and maintainable while adding powerful filtering and sorting capabilities.

1. Add filterable fields to your model

use Abbasudo\Purity\Traits\Filterable;

class Product extends Model
{
    use Filterable;

    protected $filterFields = [
        'name',
        'price',
        'category_id',
        'created_at',
    ];
}

2. Use URL-based filtering

Purity reads filters directly from the query string:

/products?filters[name][$contains]=laptop&filters[price][$gte]=500

3. Implement it in the controller

public function index()
{
    return Product::filter()->sort()->paginate(20);
}

That's it — one line in the controller, and your API supports rich filtering ($eq, $contains, $gte, $in, and more) plus sorting, without any custom query-building code.

Purity is available on GitHub: search for abbasudo/laravel-purity.