Skip to content

Commit 8937e25

Browse files
authored
[10.x] Fix runPaginationCountQuery not working properly for union queries (#52314)
* Fix runPaginationCountQuery not working properly for union queries * Added tests
1 parent 82e9dbc commit 8937e25

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,10 +3016,10 @@ protected function runPaginationCountQuery($columns = ['*'])
30163016
->get()->all();
30173017
}
30183018

3019-
$without = $this->unions ? ['orders', 'limit', 'offset'] : ['columns', 'orders', 'limit', 'offset'];
3019+
$without = $this->unions ? ['unionOrders', 'unionLimit', 'unionOffset'] : ['columns', 'orders', 'limit', 'offset'];
30203020

30213021
return $this->cloneWithout($without)
3022-
->cloneWithoutBindings($this->unions ? ['order'] : ['select', 'order'])
3022+
->cloneWithoutBindings($this->unions ? ['unionOrder'] : ['select', 'order'])
30233023
->setAggregate('count', $this->withoutSelectAliases($columns))
30243024
->get()->all();
30253025
}

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,34 @@ public function testGetCountForPaginationWithUnion()
20702070
$this->assertEquals(1, $count);
20712071
}
20722072

2073+
public function testGetCountForPaginationWithUnionOrders()
2074+
{
2075+
$builder = $this->getBuilder();
2076+
$builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id'))->latest();
2077+
2078+
$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from ((select "id" from "posts") union (select "id" from "videos")) as "temp_table"', [], true)->andReturn([['aggregate' => 1]]);
2079+
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
2080+
return $results;
2081+
});
2082+
2083+
$count = $builder->getCountForPagination();
2084+
$this->assertEquals(1, $count);
2085+
}
2086+
2087+
public function testGetCountForPaginationWithUnionLimitAndOffset()
2088+
{
2089+
$builder = $this->getBuilder();
2090+
$builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id'))->take(15)->skip(1);
2091+
2092+
$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from ((select "id" from "posts") union (select "id" from "videos")) as "temp_table"', [], true)->andReturn([['aggregate' => 1]]);
2093+
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) {
2094+
return $results;
2095+
});
2096+
2097+
$count = $builder->getCountForPagination();
2098+
$this->assertEquals(1, $count);
2099+
}
2100+
20732101
public function testWhereShortcut()
20742102
{
20752103
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)