From 757d9f95418dd6e6125ff9209269a59ab043c81e Mon Sep 17 00:00:00 2001 From: emargareten <2771355@gmail.com> Date: Fri, 9 Feb 2024 14:06:06 -0500 Subject: [PATCH 1/2] [10.x] Introduce ScopedBy attribute for models --- .../Database/Eloquent/Attributes/ScopedBy.php | 19 +++++++++++++ .../Eloquent/Concerns/HasGlobalScopes.php | 27 +++++++++++++++++++ .../DatabaseEloquentGlobalScopesTest.php | 15 +++++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/Illuminate/Database/Eloquent/Attributes/ScopedBy.php diff --git a/src/Illuminate/Database/Eloquent/Attributes/ScopedBy.php b/src/Illuminate/Database/Eloquent/Attributes/ScopedBy.php new file mode 100644 index 000000000000..747ed9e612a3 --- /dev/null +++ b/src/Illuminate/Database/Eloquent/Attributes/ScopedBy.php @@ -0,0 +1,19 @@ +getAttributes(ScopedBy::class)) + ->map(fn ($attribute) => $attribute->getArguments()) + ->flatten() + ->all(); + } + /** * Register a new global scope on the model. * diff --git a/tests/Database/DatabaseEloquentGlobalScopesTest.php b/tests/Database/DatabaseEloquentGlobalScopesTest.php index bef11fbcc854..3b5a379d4fdb 100644 --- a/tests/Database/DatabaseEloquentGlobalScopesTest.php +++ b/tests/Database/DatabaseEloquentGlobalScopesTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Database; use Illuminate\Database\Capsule\Manager as DB; +use Illuminate\Database\Eloquent\Attributes\ScopedBy; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Scope; @@ -51,6 +52,14 @@ public function testClassNameGlobalScopeIsApplied() $this->assertEquals([1], $query->getBindings()); } + public function testGlobalScopeInAttributeIsApplied() + { + $model = new EloquentGlobalScopeInAttributeTestModel; + $query = $model->newQuery(); + $this->assertSame('select * from "table" where "active" = ?', $query->toSql()); + $this->assertEquals([1], $query->getBindings()); + } + public function testClosureGlobalScopeIsApplied() { $model = new EloquentClosureGlobalScopesTestModel; @@ -233,6 +242,12 @@ public static function boot() } } +#[ScopedBy(ActiveScope::class)] +class EloquentGlobalScopeInAttributeTestModel extends Model +{ + protected $table = 'table'; +} + class ActiveScope implements Scope { public function apply(Builder $builder, Model $model) From a4e9aec4e0b096946abd57ef1c78d96c8ad8b3e3 Mon Sep 17 00:00:00 2001 From: Eliezer Margareten <46111162+emargareten@users.noreply.github.com> Date: Fri, 9 Feb 2024 16:04:44 -0500 Subject: [PATCH 2/2] Update HasGlobalScopes.php --- src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php b/src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php index 6fdb238ed4eb..0913d94b372a 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php @@ -22,7 +22,7 @@ public static function bootHasGlobalScopes() } /** - * Resolve the observe class names from the attributes. + * Resolve the global scope class names from the attributes. * * @return array */