Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Elliot_putt's avatar

Scoper and Advanced Filtering

Hi All, I have a general question on the best way to tackle a query I have.

So I am Making an advanced filter for searching through All of the Users.

This is how I would currently like to search:

  1. Filter multiple attributes at one time ('name' and newest created_at and alphabetically)

I'm currently doing it with about 10 different if statements for each request like:

 $filtered = Log::select();
 if($request->name !== null)
{
  $results = $filtered->logNameFilter($request->name);
}

But after many inputs, this isn't very efficient if anyone could just throw some ideas out that would be great.

if you would like visual input for the form and HTML here is a screenshot of the filter.

https://clpt-my.sharepoint.com/:i:/g/personal/elliot_putt_clpt_co_uk/EQs-SLcwiyJMqPJTWjwX_jQB0UqmeJymRA2vVTi4BG3OSw?e=88toS7

0 likes
4 replies
martinbean's avatar
Level 80

@elliot_putt Spatie (of course) has a package that solves this problem: https://spatie.be/docs/laravel-query-builder/v5/introduction

You build your filters (and sorts) using query string parameters. For example:

https://example.com/users?filter[name]=John&sort-created_at&sort=name

This will filter users to those who have “John” in their name, and then sort by created_at descending, then name ascending (alphabetically).

1 like
Elliot_putt's avatar

@martinbean Perfect I already use their backup and file manager why not add another haha!

Thanks for this , Last question I hope using the query string like that can you still paginate?

martinbean's avatar

Last question I hope using the query string like that can you still paginate?

@Elliot_putt Absolutely. It would just be another query string parameter:

/users?filter[name]=John&sort-created_at&sort=name&page=3

1 like

Please or to participate in this conversation.