{"record":{"id":"42f858169739475e","repo":"laravel/framework","slug":"configuration-value-for-key-s-must-be-a-string","errorCode":null,"errorMessage":"Configuration value for key [%s] must be a string, %s given.","messagePattern":"Configuration value for key \\[(.+?)\\] must be a string, (.+?) given\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Config/Repository.php","lineNumber":95,"sourceCode":"\n        return $config;\n    }\n\n    /**\n     * Get the specified string configuration value.\n     *\n     * @param  string  $key\n     * @param  (\\Closure():(string|null))|string|null  $default\n     * @return string\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function string(string $key, $default = null): string\n    {\n        $value = $this->get($key, $default);\n\n        if (! is_string($value)) {\n            throw new InvalidArgumentException(\n                sprintf('Configuration value for key [%s] must be a string, %s given.', $key, gettype($value))\n            );\n        }\n\n        return $value;\n    }\n\n    /**\n     * Get the specified integer configuration value.\n     *\n     * @param  string  $key\n     * @param  (\\Closure():(int|null))|int|null  $default\n     * @return int\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function integer(string $key, $default = null): int\n    {","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Config/Repository.php#L77-L113","documentation":"Thrown by Config\\Repository::string(string $key) when the stored value is not a string (after applying any default). This is the typed config accessor introduced for strict retrieval; is_string($value) is checked and an InvalidArgumentException reports the key and the actual gettype(). A null result from a missing key (with no default) also triggers it because null is not a string.","triggerScenarios":"Config::string('app.key') where app.key is null/missing/int/bool; reading an env-backed value that resolved to a non-string; providing a default that is itself not a string.","commonSituations":"An env var is unset so the config returns null; a config file stores an int/bool where a string was expected; typed accessor used during early bootstrap before config is loaded.","solutions":["Provide a string default: Config::string('app.locale', 'en').","Ensure the underlying config/env value is a string (quote env values: APP_KEY='base64:...').","Use Config::get() if you need to tolerate non-string values, or cast at the source."],"exampleFix":"// before\n$locale = Config::string('app.locale');\n// after\n$locale = Config::string('app.locale', 'en');\n// and in .env ensure: APP_LOCALE=en","handlingStrategy":"type-guard","validationCode":"$value = Config::get('app.locale');\nif (! is_string($value)) {\n    throw new \\InvalidArgumentException('app.locale must be a string');\n}","typeGuard":"function configIsString(string $key): bool {\n    return is_string(Config::get($key));\n}","tryCatchPattern":"try {\n    $locale = Config::string('app.locale');\n} catch (\\InvalidArgumentException $e) {\n    $locale = 'en'; // safe fallback\n}","preventionTips":["Always provide a string default to Config::string().","Quote env values and ensure config files store strings.","Validate required string config at boot and fail loudly with a clear message."],"tags":["config","type-safety","env","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}