Skip to content

Conversation

@chinleung
Copy link
Contributor

@chinleung chinleung commented Jul 17, 2024

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

@github-actions
Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@chinleung chinleung marked this pull request as ready for review July 17, 2024 14:19
@chinleung chinleung changed the title Fix runPaginationCountQuery not working properly for union queries [10.x] Fix runPaginationCountQuery not working properly for union queries Jul 17, 2024
@taylorotwell
Copy link
Member

No tests.

@chinleung
Copy link
Contributor Author

@taylorotwell I've added the tests. Do you want to reopen this pull request or should I create a new one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants