Skip to content

Commit

Permalink
[8.x] Fix clone() on EloquentBuilder (#36924)
Browse files Browse the repository at this point in the history
* Support clone() on EloquentBuilder

* Update Builder.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
Propaganistas and taylorotwell authored Apr 9, 2021
1 parent a408463 commit bf3d5e3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit bf3d5e3

Please sign in to comment.