Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use ReflectionAttribute;
use ReflectionClass;

trait HasGlobalScopes
Expand All @@ -31,8 +32,13 @@ public static function resolveGlobalScopeAttributes()
{
$reflectionClass = new ReflectionClass(static::class);

return (new Collection($reflectionClass->getAttributes(ScopedBy::class)))
->map(fn ($attribute) => $attribute->getArguments())
$attributes = (new Collection($reflectionClass->getAttributes(ScopedBy::class, ReflectionAttribute::IS_INSTANCEOF)));

foreach ($reflectionClass->getTraits() as $trait) {
$attributes->push(...$trait->getAttributes(ScopedBy::class, ReflectionAttribute::IS_INSTANCEOF));
}

return $attributes->map(fn ($attribute) => $attribute->getArguments())
->flatten()
->all();
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Database/DatabaseEloquentGlobalScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public function testGlobalScopeInAttributeIsApplied()
$this->assertEquals([1], $query->getBindings());
}

public function testGlobalScopeInInheritedAttributeIsApplied()
{
$model = new EloquentGlobalScopeInInheritedAttributeTestModel;
$query = $model->newQuery();
$this->assertSame('select * from "table" where "active" = ?', $query->toSql());
$this->assertEquals([1], $query->getBindings());
}

public function testClosureGlobalScopeIsApplied()
{
$model = new EloquentClosureGlobalScopesTestModel;
Expand Down Expand Up @@ -267,3 +275,16 @@ public function apply(Builder $builder, Model $model)
return $builder->where('active', 1);
}
}

#[ScopedBy(ActiveScope::class)]
trait EloquentGlobalScopeInInheritedAttributeTestTrait
{
//
}

class EloquentGlobalScopeInInheritedAttributeTestModel extends Model
{
use EloquentGlobalScopeInInheritedAttributeTestTrait;

protected $table = 'table';
}