🚀 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)