Skip to content

Commit a9b020e

Browse files
authored
Allow passing named arguments to dynamic scopes (#41478)
1 parent 9e7d31d commit a9b020e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ protected function callScope(callable $scope, array $parameters = [])
12461246
$originalWhereCount = is_null($query->wheres)
12471247
? 0 : count($query->wheres);
12481248

1249-
$result = $scope(...array_values($parameters)) ?? $this;
1249+
$result = $scope(...$parameters) ?? $this;
12501250

12511251
if (count((array) $query->wheres) > $originalWhereCount) {
12521252
$this->addNewWheresWithinGroup($query, $originalWhereCount);

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,28 @@ public function testQueryScopes()
832832
$this->assertEquals($builder, $result);
833833
}
834834

835+
public function testQueryDynamicScopes()
836+
{
837+
$builder = $this->getBuilder();
838+
$builder->getQuery()->shouldReceive('from');
839+
$builder->getQuery()->shouldReceive('where')->once()->with('bar', 'foo');
840+
$builder->setModel($model = new EloquentBuilderTestDynamicScopeStub);
841+
$result = $builder->dynamic('bar', 'foo');
842+
843+
$this->assertEquals($builder, $result);
844+
}
845+
846+
public function testQueryDynamicScopesNamed()
847+
{
848+
$builder = $this->getBuilder();
849+
$builder->getQuery()->shouldReceive('from');
850+
$builder->getQuery()->shouldReceive('where')->once()->with('foo', 'foo');
851+
$builder->setModel($model = new EloquentBuilderTestDynamicScopeStub);
852+
$result = $builder->dynamic(bar: 'foo');
853+
854+
$this->assertEquals($builder, $result);
855+
}
856+
835857
public function testNestedWhere()
836858
{
837859
$nestedQuery = m::mock(Builder::class);
@@ -1939,6 +1961,14 @@ public function scopeApproved($query)
19391961
}
19401962
}
19411963

1964+
class EloquentBuilderTestDynamicScopeStub extends Model
1965+
{
1966+
public function scopeDynamic($query, $foo = 'foo', $bar = 'bar')
1967+
{
1968+
$query->where($foo, $bar);
1969+
}
1970+
}
1971+
19421972
class EloquentBuilderTestHigherOrderWhereScopeStub extends Model
19431973
{
19441974
protected $table = 'table';

0 commit comments

Comments
 (0)