{"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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Cache/Repository.php#L232-L268","documentation":"Thrown by Cache\\Repository::string() when the value stored under the given key is not a string. The method calls get() then asserts is_string(); null (the implicit default for a missing key) also fails the check. It exists so typed cache accessors fail loudly instead of silently returning the wrong type.","triggerScenarios":"Calling Cache::string('key') or $repository->string('key') when the stored value is an int/array/object/bool, or when the key does not exist and no string $default was supplied (null default → 'NULL given').","commonSituations":"Config/env value cached as integer then read with string(); cache key typo or stale entry written by an older code path that stored a serialized object; forgetting that a missing key yields null.","solutions":["Pass a string default: Cache::string('key', '') so missing keys resolve to a string.","Verify the writer: ensure Cache::put('key', (string) $value) is used wherever this key is stored.","Switch to Cache::get('key') and cast/validate manually if the value type legitimately varies.","Clear the stale entry with Cache::forget('key') after correcting the writer."],"exampleFix":"// before\n$name = Cache::string('user.name');\n\n// after\n$name = Cache::string('user.name', '');\n// or fix the writer\nCache::put('user.name', (string) $user->name, now()->addHour());","handlingStrategy":"type-guard","validationCode":"$value = Cache::get('user.name');\nif (! is_string($value)) {\n    // resolve missing/wrong type before calling Cache::string()\n    $value = '';\n}","typeGuard":"function isCachedString(string $key): bool {\n    return is_string(Cache::get($key));\n}","tryCatchPattern":"try {\n    $name = Cache::string('user.name');\n} catch (\\InvalidArgumentException $e) {\n    // log and fall back\n    $name = (string) Cache::get('user.name', '');\n}","preventionTips":["Always pass a typed default to Cache::string() so a missing key resolves correctly.","Centralize each cache key in a constant and document its expected type.","Cast at the write site: Cache::put('user.name', (string) $value, $ttl)."],"tags":["cache","type-mismatch","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}