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)