Skip to content
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

[10.x] Fix runPaginationCountQuery not working properly for union queries #52314

Merged
merged 3 commits into from
Jul 30, 2024

Conversation

chinleung
Copy link
Contributor

I've created a pull request previously that has been closed due to missing tests and I've added them now.


I've recently stumbled across a bug where the total number of items displayed in Laravel Nova was incorrect after updating the index query to use unions. The total would always be equal to the page size even though there are more items in the pagination.

I started debugging the issue and found the following line Illuminate\Database\Query\Builder::runPaginationCountQuery:

$without = $this->unions ? ['orders', 'limit', 'offset'] : ['columns', 'orders', 'limit', 'offset'];

return $this->cloneWithout($without)
    ->cloneWithoutBindings($this->unions ? ['order'] : ['select', 'order'])
    ->setAggregate('count', $this->withoutSelectAliases($columns))
    ->get()->all();

As we can see from the code above, it's cloning the query without orders, limit and offset when there are unions in the query. However, if we have a look at the limit function from the same class:

public function limit($value)
{
    $property = $this->unions ? 'unionLimit' : 'limit';

    if ($value >= 0) {
        $this->$property = ! is_null($value) ? (int) $value : null;
    }

    return $this;
}

We can see that the property for unions is unionLimit and not limit.

After searching through the issues, I found something that was closed previously because they did not provide a repository to test: #38639

Therefore, I made a repository to show the bug in action: https://github.com/chinleung/laravel-bug-52170

@taylorotwell taylorotwell merged commit 8937e25 into laravel:10.x Jul 30, 2024
26 checks passed
@chinleung chinleung deleted the 10.x branch July 30, 2024 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants