laravel/framework · error · InvalidArgumentException
Configuration value for key [%s] must be a string, %s given.
Error message
Configuration value for key [%s] must be a string, %s given.
What it means
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().
Source
Thrown at src/Illuminate/Config/Repository.php:95
return $config;
}
/**
* Get the specified string configuration value.
*
* @param string $key
* @param (\Closure():(string|null))|string|null $default
* @return string
*
* @throws \InvalidArgumentException
*/
public function string(string $key, $default = null): string
{
$value = $this->get($key, $default);
if (! is_string($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be a string, %s given.', $key, gettype($value))
);
}
return $value;
}
/**
* Get the specified integer configuration value.
*
* @param string $key
* @param (\Closure():(int|null))|int|null $default
* @return int
*
* @throws \InvalidArgumentException
*/
public function integer(string $key, $default = null): int
{View on GitHub (pinned to bd6b5437e6)
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.
Example fix
// before
$region = config()->string('app.aws_region');
// after
$region = config()->string('app.aws_region', 'us-east-1'); Defensive patterns
Strategy: validation
Validate before calling
$value = config()->get($key);
if (! is_string($value)) {
throw new \RuntimeException("Config $key must be string, got ".gettype($value));
}
// or simply provide a default:
config()->string($key, 'fallback'); Type guard
function isStringConfig(\Illuminate\Config\Repository $config, string $key): bool {
return is_string($config->get($key));
} Try / catch
try {
$value = config()->string($key);
} catch (\InvalidArgumentException $e) {
$value = 'fallback';
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Configuration value for key [%s] must be an integer, %s give
- Configuration value for key [%s] must be a float, %s given.
- Configuration value for key [%s] must be a boolean, %s given
- Configuration value for key [%s] must be an array, %s given.
- The given password does not match the current password.
AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06).
Data as JSON: /data/errors/42f858169739475e.json.
Report an issue: GitHub.