From bf3d5e319f0bd928a493222adb1acd9d23c1db68 Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Fri, 9 Apr 2021 14:48:38 +0200 Subject: [PATCH] [8.x] Fix clone() on EloquentBuilder (#36924) * Support clone() on EloquentBuilder * Update Builder.php Co-authored-by: Taylor Otwell --- src/Illuminate/Database/Eloquent/Builder.php | 10 ++++++++++ tests/Database/DatabaseEloquentBuilderTest.php | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index ca84d06cdbc1..a8f2fb87842c 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1608,6 +1608,16 @@ protected static function registerMixin($mixin, $replace) } } + /** + * Clone the Eloquent query builder. + * + * @return static + */ + public function clone() + { + return clone $this; + } + /** * Force a clone of the underlying query builder when cloning. * diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 0809f8e0fe98..682545ff9b98 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -1497,6 +1497,18 @@ public function testWithCastsMethod() $builder->withCasts(['foo' => 'bar']); } + public function testClone() + { + $query = new BaseBuilder(m::mock(ConnectionInterface::class), new Grammar, m::mock(Processor::class)); + $builder = new Builder($query); + $builder->select('*')->from('users'); + $clone = $builder->clone()->where('email', 'foo'); + + $this->assertNotSame($builder, $clone); + $this->assertSame('select * from "users"', $builder->toSql()); + $this->assertSame('select * from "users" where "email" = ?', $clone->toSql()); + } + protected function mockConnectionForModel($model, $database) { $grammarClass = 'Illuminate\Database\Query\Grammars\\'.$database.'Grammar';