Skip to content

Commit 166d52b

Browse files
committed
13.x-scopedby-inheritence
ensure it works with inheritence
1 parent 8aa511e commit 166d52b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
7+
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Scope;
89
use Illuminate\Support\Arr;
910
use Illuminate\Support\Collection;
@@ -38,8 +39,15 @@ public static function resolveGlobalScopeAttributes()
3839
$attributes->push(...$trait->getAttributes(ScopedBy::class, ReflectionAttribute::IS_INSTANCEOF));
3940
}
4041

42+
$isEloquentGrandchild = is_subclass_of(static::class, Model::class)
43+
&& get_parent_class(static::class) !== Model::class;
44+
4145
return $attributes->map(fn ($attribute) => $attribute->getArguments())
4246
->flatten()
47+
->when($isEloquentGrandchild, function (Collection $attributes) {
48+
return (new Collection(get_parent_class(static::class)::resolveGlobalScopeAttributes()))
49+
->merge($attributes);
50+
})
4351
->all();
4452
}
4553

tests/Database/DatabaseEloquentGlobalScopesTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public function testGlobalScopeInInheritedAttributeIsApplied()
6868
$this->assertEquals([1], $query->getBindings());
6969
}
7070

71+
public function testGlobalScopeInParentClassAttributeIsApplied()
72+
{
73+
$model = new EloquentGlobalScopeInAttributeChildTestModel;
74+
$query = $model->newQuery();
75+
$this->assertSame('select * from "table" where "active" = ?', $query->toSql());
76+
$this->assertEquals([1], $query->getBindings());
77+
}
78+
7179
public function testClosureGlobalScopeIsApplied()
7280
{
7381
$model = new EloquentClosureGlobalScopesTestModel;
@@ -325,3 +333,14 @@ class EloquentGlobalScopeInInheritedAttributeTestModel extends Model
325333

326334
protected $table = 'table';
327335
}
336+
337+
#[ScopedBy(ActiveScope::class)]
338+
class EloquentGlobalScopeInAttributeParentTestModel extends Model
339+
{
340+
protected $table = 'table';
341+
}
342+
343+
class EloquentGlobalScopeInAttributeChildTestModel extends EloquentGlobalScopeInAttributeParentTestModel
344+
{
345+
//
346+
}

0 commit comments

Comments
 (0)