DEV Community

Cover image for Stop Writing Messy Queries in Laravel β€” Meet kettasoft/filterable πŸš€
Abdalrhman Emad Saad
Abdalrhman Emad Saad

Posted on

Stop Writing Messy Queries in Laravel β€” Meet kettasoft/filterable πŸš€

πŸš€ 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();
}
Enter fullscreen mode Exit fullscreen mode

…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();
Enter fullscreen mode Exit fullscreen mode

βœ… With filterable:

$posts = Post::filter(new PostFilter)->paginate();
Enter fullscreen mode Exit fullscreen mode

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}%");
    }
}
Enter fullscreen mode Exit fullscreen mode

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');
});
Enter fullscreen mode Exit fullscreen mode

Request:

/posts?sort=-title
Enter fullscreen mode Exit fullscreen mode

Query generated:

select * from posts order by title desc
Enter fullscreen mode Exit fullscreen mode

🌍 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
Enter fullscreen mode Exit fullscreen mode

πŸ“ 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)