π Supercharge Your Laravel Queries with kettasoft/filterable
When building Laravel applications, one of the most common pain points is handling dynamic filtering from requests.
If you've ever written controllers like this:
public function index(Request $request)
{
$query = Post::query();
if ($request->has('status')) {
$query->where('status', $request->status);
}
if ($request->has('author_id')) {
$query->where('author_id', $request->author_id);
}
if ($request->has('search')) {
$query->where('title', 'like', '%' . $request->search . '%');
}
return $query->paginate();
}
β¦you know how quickly it becomes messy, repetitive, and hard to maintain. π΅
β¨ Meet kettasoft/filterable
This package makes request-based filtering simple, reusable, and powerful.
Instead of manually checking request inputs, you can write clean filter classes and attach them to your models.
β‘ Before vs After
β Without filterable
:
$posts = Post::query();
if (request('status')) {
$posts->where('status', request('status'));
}
if (request('author_id')) {
$posts->where('author_id', request('author_id'));
}
$posts = $posts->paginate();
β
With filterable
:
$posts = Post::filter(new PostFilter)->paginate();
Your filters are now reusable, testable, and clean.
π Example Filter
use Kettasoft\Filterable\Filter;
class PostFilter extends Filter
{
public function status($value)
{
$this->query->where('status', $value);
}
public function author_id($value)
{
$this->query->where('author_id', $value);
}
public function search($value)
{
$this->query->where('title', 'like', "%{$value}%");
}
}
Now, calling /posts?status=published&search=laravel
automatically applies the filters! π
π§ Sorting Support
It even supports sorting with customization:
Filterable::addSorting(PostFilter::class, function (Sortable $sort) {
$sort->allow(['id', 'title', 'created_at'])
->default('created_at', 'desc');
});
Request:
/posts?sort=-title
Query generated:
select * from posts order by title desc
π Why use kettasoft/filterable
?
- β Clean & maintainable queries
- β Reusable filter classes
- β Global & local filters
- β
Sorting with direction maps (
-field = desc
) - β Support for multi-sort
π¦ Installation
composer require kettasoft/filterable
π Final Thoughts
If you want to keep your controllers clean and make your queries more expressive, powerful, and reusable, give kettasoft/filterable
a try.
It saves time, improves maintainability, and makes your codebase a lot more fun to work with. π
Top comments (0)