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

JJK's avatar
Level 1

A custom query builder to support with, filters, sorting and pagination

Hello,

I'm currently working on a project that has somewhat complicated filters, sorting and relations on every view.

The views cover:

  • sorting by one or multiple columns
  • basic pagination
  • filtering with flags:
    • contains (for text contained within text like 'na' in 'Banana')
    • range (for numerics, like 5 being between 1 and 10)
    • ignore case (for text, to ignore case).

Originally, I was planning on using: https://docs.spatie.be/laravel-query-builder/v2/introduction/ but it doesn't cover passing flags to the filter.

Could anyone push me in the right direction? I'm thinking that expanding QueryBuilder would be the suggested approach.

0 likes
6 replies
jlrdw's avatar

Done many times: Punch into google

site:laracasts.com eloquent filtering and pagination

Explanation, google search is more powerful than the search here.

Tweak search as needed. And there are past in depth great discussions on this.

JJK's avatar
Level 1

Neither of these discuss how to implement flags that you can pass along with the filter.

jlrdw's avatar

Of course some things you have to do yourself, but those past discussions should point you in the correct direction.

As far as 'na' in 'Banana' that's just using like operator.

To get a better grasp on sql and writing various queries I suggest this site:

https://www.mysqltutorial.org/

And like is exampled in the docs:

$users = DB::table('users')
                ->where('name', 'like', 'T%')
                ->get();

Same for numbers between, greater than, less than, all can be done in sql.

You'd be surprised how much you will learn by working the examples that Taylor provided in the documentation.

JJK's avatar
Level 1

The problem isn't builiding a query, the problem is builiding a universal system where I can just send 'include, filter, sort' in Request and it knows how to handle them.

jlrdw's avatar

Maybe you should look at one of the datatables implementations. I don't use myself I usually just write my own queries.

Please or to participate in this conversation.