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

saimiris's avatar

Order or filter paginated query results

Hi there,

After doing a paginated query on a model, I manually add some custom fields to my query result collection. These fields are calculated based on queries made on an external database so there is no way I can merge all this in a single query, which obviously would solve everything.

So I would like to know if there is way either to post-filter wither to post-sort a query result, which is paginated.

Thanks!

0 likes
6 replies
bashy's avatar

Collect the data (array), then make your own pagination? You can use the LengthAwarePaginator class

saimiris's avatar

Hi @bashy

Thanks for helping.

I see your point and that could work BUT the field-calculation process takes ages so I really need to limit myself to an already-paginated query result.

saimiris's avatar

I'm sorry but I still don't understand your answers. I already used the LengthAwarePaginator class to paginate an array on another project but I can't see how this will resolve my issue.

Summary: query with a lot of results -> additional custom fields manually calculated* -> sorting the results by one of the manually-calculated field -> paginating.

Issue: This is way too long to process due to my big number of query results. So I would like to calculate the custom fields only on a subset of query results, to calculate only the custom fields I need on the first page of pagination. BUT obviously I need to calculate all the fields before I get such an info. Endless issue then... :-(

  • The manually fields are calculated querying an external database, so there is no way I can do a JOIN query.
Snapey's avatar

How many rows/pages are you talking about?

jlrdw's avatar

You cannot think outside of the box you have to remove the Box.

At a trucking company I would dump monthly the data and on local database connected via odbc connection using Microsoft Access I would do the actual month end reports.

But there are other Alternatives besides Microsoft Access because a large month-end reports on a desktop database is a zillion times more efficient than anything you can ever ever ever do on the web.

Heck I miss the old debase 3 days which was my first thing I ever learned to program in the late 80s.

Please or to participate in this conversation.