Skip to content

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

Merged
taylorotwell merged 3 commits into
laravel:10.xfrom
chinleung:10.x
Jul 30, 2024
Merged

[10.x] Fix runPaginationCountQuery not working properly for union queries#52314
taylorotwell merged 3 commits into
laravel:10.xfrom
chinleung:10.x

Conversation

@chinleung

Copy link
Copy Markdown
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
@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.

2 participants