{"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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Config/Repository.php#L77-L113","documentation":"Thrown by Config Repository::string() when the resolved config value is not a string. The typed accessor methods (string/integer/float/boolean/array/json) coerce-or-validate; string() is strict and rejects non-strings (including null when no default is given). InvalidArgumentException reports the key and the actual gettype().","triggerScenarios":"Calling config()->string('app.key') when app.key is null, an array, or an integer. Also Config::string('services.stripe.secret') when the env var is unset (null) and no default is supplied.","commonSituations":"Env vars not loaded (missing .env, wrong APP_ENV), returning null. Config files returning arrays where a scalar was expected. Typoed keys resolving to the implicit null default.","solutions":["Supply a string default: config()->string('app.locale', 'en').","Ensure the underlying env var / config value is actually set and is a string.","Use the correctly typed accessor (integer(), float(), boolean(), array()) for the actual value type."],"exampleFix":"// before\n$region = config()->string('app.aws_region');\n\n// after\n$region = config()->string('app.aws_region', 'us-east-1');","handlingStrategy":"validation","validationCode":"$value = config()->get($key);\nif (! is_string($value)) {\n    throw new \\RuntimeException(\"Config $key must be string, got \".gettype($value));\n}\n// or simply provide a default:\nconfig()->string($key, 'fallback');","typeGuard":"function isStringConfig(\\Illuminate\\Config\\Repository $config, string $key): bool {\n    return is_string($config->get($key));\n}","tryCatchPattern":"try {\n    $value = config()->string($key);\n} catch (\\InvalidArgumentException $e) {\n    $value = 'fallback';\n}","preventionTips":["Always pass a typed default to the typed accessors: string(), integer(), float(), boolean(), array().","Confirm required env vars are present in every environment (.env checked in, APP_ENV correct).","Use the accessor matching the real value type; don't force string() on array/int config."],"tags":["laravel","config","type-safety","env","validation"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}