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
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public static function addGlobalScope($scope, $implementation = null)
return static::$globalScopes[static::class][spl_object_hash($scope)] = $scope;
} elseif ($scope instanceof Scope) {
return static::$globalScopes[static::class][get_class($scope)] = $scope;
} elseif (is_string($scope) && class_exists($scope) && is_subclass_of($scope, Scope::class)) {
return static::$globalScopes[static::class][$scope] = new $scope;
}

throw new InvalidArgumentException('Global scope must be an instance of Closure or Scope.');
throw new InvalidArgumentException('Global scope must be an instance of Closure or Scope or be a class name of a class extending '.Scope::class);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Database/DatabaseEloquentGlobalScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function testGlobalScopeCanBeRemoved()
$this->assertEquals([], $query->getBindings());
}

public function testClassNameGlobalScopeIsApplied()
{
$model = new EloquentClassNameGlobalScopesTestModel;
$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 @@ -190,6 +198,18 @@ public static function boot()
}
}

class EloquentClassNameGlobalScopesTestModel extends Model
{
protected $table = 'table';

public static function boot()
{
static::addGlobalScope(ActiveScope::class);

parent::boot();
}
}

class ActiveScope implements Scope
{
public function apply(Builder $builder, Model $model)
Expand Down