abbasudo/laravel-purity
Laravel Purity adds elegant filtering and sorting to Eloquent queries via a simple filter() method. Let frontend users drive complex query conditions using URL query string parameters (e.g., filters[title][$contains]=Purity), with minimal boilerplate.
By default, Purity allows every database column and all model relations (that have a defined return type) to be filtered.
// App\Models\User
public function posts(): Illuminate\Database\Eloquent\Relations\HasMany // This is mandatory
{
return $this->hasMany(Post::class);
}
you can overwrite the allowed columns as follows:
// App\Models\User
protected $filterFields = [
'email',
'mobile',
'posts', // relation
];
protected $sortFields = [
'name',
'mobile',
];
any field other than email, mobile, or posts will be rejected when filtering.
to overwrite allowed fields in the controller add filterFields or sortFields before calling filter or sort method.
Post::filterFields('title', 'created_at')->filter()->get();
Post::sortFields('created_at', 'updated_at')->sort()->get();
::: tip filterFields and sortFields will overwrite fields defined in the model. :::
How can I help you explore Laravel packages today?