diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 13d7dfeaaf55..b713060a9bc3 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -171,6 +171,13 @@ class Builder */ protected $backups = []; + /** + * The binding backups currently in use. + * + * @var array + */ + protected $bindingBackups = []; + /** * All of the available clause operators. * @@ -1463,6 +1470,12 @@ protected function backupFieldsForCount() $this->{$field} = null; } + + foreach (['order', 'select'] as $key) { + $this->bindingBackups[$key] = $this->bindings[$key]; + + $this->bindings[$key] = []; + } } /** @@ -1476,7 +1489,12 @@ protected function restoreFieldsForCount() $this->{$field} = $this->backups[$field]; } + foreach (['order', 'select'] as $key) { + $this->bindings[$key] = $this->bindingBackups[$key]; + } + $this->backups = []; + $this->bindingBackups = []; } /** diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 5a3826ca4f09..307e0c31287d 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -535,6 +535,19 @@ public function testLimitsAndOffsets() $this->assertEquals('select * from "users" limit 15 offset 0', $builder->toSql()); } + public function testGetCountForPaginationWithBindings() + { + $builder = $this->getBuilder(); + $builder->from('users')->selectSub(function($q) { $q->select('body')->from('posts')->where('id', 4); }, 'post'); + + $builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', [], true)->andReturn([['aggregate' => 1]]); + $builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function ($builder, $results) { return $results; }); + + $count = $builder->getCountForPagination(); + $this->assertEquals(1, $count); + $this->assertEquals([4], $builder->getBindings()); + } + public function testWhereShortcut() { $builder = $this->getBuilder();