Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3016,10 +3016,10 @@ protected function runPaginationCountQuery($columns = ['*'])
->get()->all();
}

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

return $this->cloneWithout($without)
->cloneWithoutBindings($this->unions ? ['order'] : ['select', 'order'])
->cloneWithoutBindings($this->unions ? ['unionOrder'] : ['select', 'order'])
->setAggregate('count', $this->withoutSelectAliases($columns))
->get()->all();
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,34 @@ public function testGetCountForPaginationWithUnion()
$this->assertEquals(1, $count);
}

public function testGetCountForPaginationWithUnionOrders()
{
$builder = $this->getBuilder();
$builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id'))->latest();

$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 testGetCountForPaginationWithUnionLimitAndOffset()
{
$builder = $this->getBuilder();
$builder->from('posts')->select('id')->union($this->getBuilder()->from('videos')->select('id'))->take(15)->skip(1);

$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();
Expand Down