diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 524163829406..4d6e8a6917ac 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2130,8 +2130,10 @@ public function getCountForPagination($columns = ['*']) */ protected function runPaginationCountQuery($columns = ['*']) { - return $this->cloneWithout(['columns', 'orders', 'limit', 'offset']) - ->cloneWithoutBindings(['select', 'order']) + $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(); } @@ -2444,8 +2446,8 @@ public function average($column) */ public function aggregate($function, $columns = ['*']) { - $results = $this->cloneWithout(['columns']) - ->cloneWithoutBindings(['select']) + $results = $this->cloneWithout($this->unions ? [] : ['columns']) + ->cloneWithoutBindings($this->unions ? [] : ['select']) ->setAggregate($function, $columns) ->get($columns); diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 8ea54092cb3e..70b462b1331c 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -843,6 +843,12 @@ public function testUnionAggregate() $builder->getProcessor()->shouldReceive('processSelect')->once(); $builder->from('posts')->union($this->getMySqlBuilder()->from('videos'))->count(); + $expected = 'select count(*) as aggregate from ((select `id` from `posts`) union (select `id` from `videos`)) as `temp_table`'; + $builder = $this->getMySqlBuilder(); + $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true); + $builder->getProcessor()->shouldReceive('processSelect')->once(); + $builder->from('posts')->select('id')->union($this->getMySqlBuilder()->from('videos')->select('id'))->count(); + $expected = 'select count(*) as aggregate from (select * from "posts" union select * from "videos") as "temp_table"'; $builder = $this->getPostgresBuilder(); $builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true); @@ -1095,6 +1101,20 @@ public function testGetCountForPaginationWithColumnAliases() $this->assertEquals(1, $count); } + public function testGetCountForPaginationWithUnion() + { + $builder = $this->getBuilder(); + $builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id')); + + $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]]); + $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { + return $results; + }); + + $count = $builder->getCountForPagination(); + $this->assertEquals(1, $count); + } + public function testWhereShortcut() { $builder = $this->getBuilder();