From a9b020ecb3e31aea2fe6efb458b8e21c45d72ecd Mon Sep 17 00:00:00 2001 From: Jesper Noordsij <45041769+jnoordsij@users.noreply.github.com> Date: Mon, 14 Mar 2022 19:14:21 +0100 Subject: [PATCH] Allow passing named arguments to dynamic scopes (#41478) --- src/Illuminate/Database/Eloquent/Builder.php | 2 +- .../Database/DatabaseEloquentBuilderTest.php | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index f93b217f9bc7..365247f69f68 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -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); diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index fc22a94eb3f4..c22010595d93 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -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); @@ -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';