Skip to content

Commit

Permalink
Allow passing named arguments to dynamic scopes (#41478)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnoordsij committed Mar 14, 2022
1 parent 9e7d31d commit a9b020e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Expand Up @@ -1246,7 +1246,7 @@ protected function callScope(callable $scope, array $parameters = [])
$originalWhereCount = is_null($query->wheres)
? 0 : count($query->wheres);

$result = $scope(...array_values($parameters)) ?? $this;
$result = $scope(...$parameters) ?? $this;

if (count((array) $query->wheres) > $originalWhereCount) {
$this->addNewWheresWithinGroup($query, $originalWhereCount);
Expand Down
30 changes: 30 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Expand Up @@ -832,6 +832,28 @@ public function testQueryScopes()
$this->assertEquals($builder, $result);
}

public function testQueryDynamicScopes()
{
$builder = $this->getBuilder();
$builder->getQuery()->shouldReceive('from');
$builder->getQuery()->shouldReceive('where')->once()->with('bar', 'foo');
$builder->setModel($model = new EloquentBuilderTestDynamicScopeStub);
$result = $builder->dynamic('bar', 'foo');

$this->assertEquals($builder, $result);
}

public function testQueryDynamicScopesNamed()
{
$builder = $this->getBuilder();
$builder->getQuery()->shouldReceive('from');
$builder->getQuery()->shouldReceive('where')->once()->with('foo', 'foo');
$builder->setModel($model = new EloquentBuilderTestDynamicScopeStub);
$result = $builder->dynamic(bar: 'foo');

$this->assertEquals($builder, $result);
}

public function testNestedWhere()
{
$nestedQuery = m::mock(Builder::class);
Expand Down Expand Up @@ -1939,6 +1961,14 @@ public function scopeApproved($query)
}
}

class EloquentBuilderTestDynamicScopeStub extends Model
{
public function scopeDynamic($query, $foo = 'foo', $bar = 'bar')
{
$query->where($foo, $bar);
}
}

class EloquentBuilderTestHigherOrderWhereScopeStub extends Model
{
protected $table = 'table';
Expand Down

0 comments on commit a9b020e

Please sign in to comment.