{"record":{"id":"c84b5edf6f0d0575","repo":"laravel/framework","slug":"cache-value-for-key-s-must-be-a-string-s-give","errorCode":null,"errorMessage":"Cache value for key [%s] must be a string, %s given.","messagePattern":"Cache value for key \\[(.+?)\\] must be a string, (.+?) given\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Cache/Repository.php","lineNumber":250,"sourceCode":"        });\n    }\n\n    /**\n     * Retrieve a string item from the cache.\n     *\n     * @param  \\UnitEnum|string  $key\n     * @param  (\\Closure():(string|null))|string|null  $default\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function string($key, $default = null): string\n    {\n        $key = enum_value($key);\n\n        $value = $this->get($key, $default);\n\n        if (! is_string($value)) {\n            throw new InvalidArgumentException(\n                sprintf('Cache value for key [%s] must be a string, %s given.', $key, gettype($value))\n            );\n        }\n\n        return $value;\n    }\n\n    /**\n     * Retrieve an integer item from the cache.\n     *\n     * @param  \\UnitEnum|string  $key\n     * @param  (\\Closure():(int|null))|int|null  $default\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function integer($key, $default = null): int\n    {\n        $key = enum_value($key);","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Cache/Repository.php#L232-L268","documentation":"Thrown by Repository::string() when the value fetched from the cache (or the resolved default) is not a PHP string. The method calls get($key, $default); if the key is absent and no default is supplied, get returns null, which fails is_string(). This is a hard type assertion with no coercion.","triggerScenarios":"Calling Cache::string('key') (or the 'string' typed accessor) when the stored value is an int/bool/array/null, or when the key is missing and $default is omitted or resolves to a non-string.","commonSituations":"Migrating from get() to the typed string() accessor without supplying a default; storing numeric IDs as integers then reading them as strings; cache miss returning null.","solutions":["Pass a string default: Cache::string('key', '') so misses resolve to a string.","Store the value as a string at write time (cast with (string) $value before Cache::put).","Use Cache::get() if you intentionally accept mixed/null types."],"exampleFix":"// before\n$name = Cache::string('user.name'); // throws if missing\n// after\n$name = Cache::string('user.name', '');","handlingStrategy":"validation","validationCode":"// Before calling Cache::string, ensure a string default is provided or check existence.\nif (Cache::has($key)) {\n    $val = Cache::get($key);\n    if (! is_string($val)) {\n        // normalize or skip\n    }\n}\n// Simplest: always pass a default.\n$val = Cache::string($key, '');","typeGuard":"function isCacheStringable(mixed $value): bool { return is_string($value); }","tryCatchPattern":"try {\n    $name = Cache::string('user.name');\n} catch (\\InvalidArgumentException $e) {\n    $name = '';\n    logger()->warning('Cache string read failed', ['exception' => $e]);\n}","preventionTips":["Always pass a typed default to the typed Cache accessors.","Normalize values to the expected PHP type before Cache::put().","Prefer Cache::get() with an explicit cast when the stored type is uncertain."],"tags":["cache","type-mismatch","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}