Skip to content

Run a more robust count query that supports "group by" #1382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

lex0r
Copy link
Contributor

@lex0r lex0r commented Jul 8, 2025

Fixes #454

@mjauvin
Copy link
Member

mjauvin commented Jul 8, 2025

@lex0r thanks for this PR.

Can you give a bit more details on exactly what those methods do, I am not familiar with them.

@mjauvin mjauvin self-assigned this Jul 8, 2025
@mjauvin mjauvin added the maintenance PRs that fix bugs, are translation changes or make only minor changes label Jul 8, 2025
@mjauvin mjauvin added this to the 1.2.8 milestone Jul 8, 2025
@mjauvin mjauvin added needs review Issues/PRs that require a review from a maintainer needs test case Issues/PRs that need a test case to be implemented labels Jul 8, 2025
@lex0r
Copy link
Contributor Author

lex0r commented Jul 8, 2025

@mjauvin both the original count() method and getCountForPagination() are available in Illuminate\Database\Query\Builder, but the main difference is that count() is much simpler and I suppose was never meant to be used for pagination, while the second one has support for more complex queries that may have grouping.

@mjauvin
Copy link
Member

mjauvin commented Jul 8, 2025

@lex0r what about toBase()?

Also, unit test for this would be very appreciated.

@damsfx
Copy link
Contributor

damsfx commented Jul 8, 2025

@lex0r what about toBase()?

toBase() will retrieve the data from the database but it will not prepare the associated Eloquent models.
If you are not really going to use features like Mutators, Relationships of Eloquent and loading a large amount of data, then the toBase function can be a game changer in performances.

@mjauvin
Copy link
Member

mjauvin commented Jul 8, 2025

I'm worried that the code here will have a hard time because of this:

$records = $query->{$method}($this->recordsPerPage, $currentPageNumber);

@lex0r
Copy link
Contributor Author

lex0r commented Jul 9, 2025

@lex0r what about toBase()?

toBase() will retrieve the data from the database but it will not prepare the associated Eloquent models. If you are not really going to use features like Mutators, Relationships of Eloquent and loading a large amount of data, then the toBase function can be a game changer in performances.

The good news is that we only need to run a count query, so not interested in any actual record data.

I'm worried that the code here will have a hard time because of this:

$records = $query->{$method}($this->recordsPerPage, $currentPageNumber);

@mjauvin not sure I'm following your concern. The code I suggested is absolutely pagination friendly, it's even in the name of the method. Surely it will work whatever pagination method is used?

@lex0r
Copy link
Contributor Author

lex0r commented Jul 9, 2025

@lex0r what about toBase()?

Also, unit test for this would be very appreciated.

I would expect a unit test that checks for what records are returned based on the requested page to already exist. If it doesn't exist then it's probably not very important, but even if it is, I can't promise I will have time to look into creating a test for pagination unless there's some ground work that has been done and it won't take long to implement :)

@mjauvin mjauvin added accepted Issues that have been accepted by the maintainers for inclusion needs test case Issues/PRs that need a test case to be implemented and removed needs review Issues/PRs that require a review from a maintainer needs test case Issues/PRs that need a test case to be implemented labels Jul 9, 2025
@mjauvin mjauvin modified the milestones: 1.2.8, 1.2.9 Jul 10, 2025
@LukeTowers
Copy link
Member

@lex0r if you aren't able to create a unit test case for this can you create a PR in the test plugin that replicates the original issue and provides instructions for testing this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Issues that have been accepted by the maintainers for inclusion maintenance PRs that fix bugs, are translation changes or make only minor changes needs test case Issues/PRs that need a test case to be implemented
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When using listExtendQuery with groupby, paginate seems not work
4 participants