{"record":{"id":"c8ca1cff330e2c6c","repo":"laravel/framework","slug":"global-scope-must-be-an-instance-of-closure-or-sco","errorCode":null,"errorMessage":"Global scope must be an instance of Closure or Scope or be a class name of a class extending Illuminate\\Database\\Eloquent\\Scope","messagePattern":"Global scope must be an instance of Closure or Scope or be a class name of a class extending Illuminate\\\\Database\\\\Eloquent\\\\Scope","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php","lineNumber":75,"sourceCode":"     * @param  \\Illuminate\\Database\\Eloquent\\Scope|(\\Closure(\\Illuminate\\Database\\Eloquent\\Builder<static>): mixed)|string  $scope\n     * @param  \\Illuminate\\Database\\Eloquent\\Scope|(\\Closure(\\Illuminate\\Database\\Eloquent\\Builder<static>): mixed)|null  $implementation\n     * @return mixed\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public static function addGlobalScope($scope, $implementation = null)\n    {\n        if (is_string($scope) && ($implementation instanceof Closure || $implementation instanceof Scope)) {\n            return static::$globalScopes[static::class][$scope] = $implementation;\n        } elseif ($scope instanceof Closure) {\n            return static::$globalScopes[static::class][spl_object_hash($scope)] = $scope;\n        } elseif ($scope instanceof Scope) {\n            return static::$globalScopes[static::class][get_class($scope)] = $scope;\n        } elseif (is_string($scope) && class_exists($scope) && is_subclass_of($scope, Scope::class)) {\n            return static::$globalScopes[static::class][$scope] = new $scope;\n        }\n\n        throw new InvalidArgumentException('Global scope must be an instance of Closure or Scope or be a class name of a class extending '.Scope::class);\n    }\n\n    /**\n     * Register multiple global scopes on the model.\n     *\n     * @param  array  $scopes\n     * @return void\n     */\n    public static function addGlobalScopes(array $scopes)\n    {\n        foreach ($scopes as $key => $scope) {\n            if (is_string($key)) {\n                static::addGlobalScope($key, $scope);\n            } else {\n                static::addGlobalScope($scope);\n            }\n        }\n    }","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Concerns/HasGlobalScopes.php#L57-L93","documentation":"Thrown by Model::addGlobalScope() when the arguments do not match any accepted signature. addGlobalScope accepts: a (string identifier, Closure|Scope) pair, a lone Closure, a lone Scope instance, or a string class name of a Scope subclass. Any other combination (e.g. a string with a non-Scope implementation, a random object, or a class string that is not a Scope subclass) hits the final else and throws.","triggerScenarios":"Calling addGlobalScope('name', $notAClosureOrScope); addGlobalScope(SomeClass::class) where SomeClass does not extend Illuminate\\Database\\Eloquent\\Scope; addGlobalScope($string, $string); or passing an instance whose class is not a Scope. The check cascade rejects all of these.","commonSituations":"Refactoring a scope from a Closure to a class but forgetting to make it extend Scope; passing a builder macro or query callback where a Scope is expected; passing a string identifier without a corresponding implementation; namespace mismatch on a Scope subclass.","solutions":["Make a class-based scope extend Illuminate\\Database\\Eloquent\\Scope and implement apply($builder, $model).","Pass a Closure as the implementation when using a string identifier: addGlobalScope('active', fn (Builder $q) => $q->where('active', true)).","Pass a single Scope instance: addGlobalScope(new ActiveScope).","Double-check argument order and types against the four accepted signatures."],"exampleFix":"// before\nUser::addGlobalScope('active', 'where active = 1'); // string is not Closure|Scope\n\n// after\nUser::addGlobalScope('active', function (Builder $query) {\n    $query->where('active', true);\n});","handlingStrategy":"validation","validationCode":"use Illuminate\\Database\\Eloquent\\Scope;\nuse Closure;\n\n$scope = 'active';\n$impl = fn (\\Illuminate\\Database\\Eloquent\\Builder $q) => $q->where('active', true);\n\n$ok = (is_string($scope) && ($impl instanceof Closure || $impl instanceof Scope))\n    || $scope instanceof Closure\n    || $scope instanceof Scope\n    || (is_string($scope) && class_exists($scope) && is_subclass_of($scope, Scope::class));\n\nif (! $ok) {\n    throw new \\LogicException('Invalid global scope signature');\n}\nUser::addGlobalScope($scope, $impl);","typeGuard":"function isValidScopeSignature(mixed $scope, mixed $implementation = null): bool\n{\n    if (is_string($scope) && ($implementation instanceof \\Closure || $implementation instanceof \\Illuminate\\Database\\Eloquent\\Scope)) {\n        return true;\n    }\n    if ($scope instanceof \\Closure || $scope instanceof \\Illuminate\\Database\\Eloquent\\Scope) {\n        return true;\n    }\n    return is_string($scope) && class_exists($scope) && is_subclass_of($scope, \\Illuminate\\Database\\Eloquent\\Scope::class);\n}","tryCatchPattern":null,"preventionTips":["Class-based scopes must `extends \\Illuminate\\Database\\Eloquent\\Scope` and implement apply().","Prefer passing a Closure for ad-hoc scopes, or a Scope instance for reusable ones.","Match the argument order exactly to one of the four documented signatures."],"tags":["eloquent","global-scope","query","configuration"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}