laravel/framework · error · InvalidArgumentException
Configuration value for key
Error message
Configuration value for key [%s] must be an integer, %s given.
What it means
Thrown by Config\Repository::integer(string $key) when the stored value is not an int. The typed accessor checks is_int($value) strictly; a numeric string from env (e.g. '5'), a float, bool, or null all fail and throw InvalidArgumentException with the key and gettype(). Unlike some accessors this does not coerce — the value must already be a PHP int.
Solutions
- Cast the env value in the config file: env('CACHE_TTL', 300) with an int default, or (int) env(...).
- Provide an int default: Config::integer('cache.ttl', 300).
- Use Config::get() and cast at the call site if you cannot change the config source.
Example fix
// before
// config file: 'ttl' => env('CACHE_TTL'), // env returns string|null
$ttl = Config::integer('cache.ttl');
// after
// config file: 'ttl' => (int) env('CACHE_TTL', 300),
$ttl = Config::integer('cache.ttl', 300); Defensive patterns
Strategy: type-guard
Validate before calling
$value = Config::get('cache.ttl');
if (! is_int($value)) {
throw new \InvalidArgumentException('cache.ttl must be int');
} Type guard
function configIsInt(string $key): bool {
return is_int(Config::get($key));
} Try / catch
try {
$ttl = Config::integer('cache.ttl');
} catch (\InvalidArgumentException $e) {
$ttl = 300; // safe default
} Prevention
- Cast env-backed values to int in config files: (int) env('KEY', 300).
- Provide an int default to Config::integer().
- Remember env() always returns string|null — never assume int without a cast.
When it happens
Trigger: Config::integer('cache.ttl') where the env/config stores '300' (string) or 300.0 (float); reading a value that is null because the key is absent and no default is given.
Common situations: Env vars are always strings, so an integer config backed by env() without an explicit cast fails; mixing integer() with values typed elsewhere as strings; missing default for an optional key.
Related errors
- Configuration value for key
- Configuration value for key
- Collection should only include
- A driver must be specified.
- Ably error
AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11).
Data as JSON: /api/errors/a96db52e1e475077.
Report an issue: GitHub.
Appendix: source
Thrown at src/Illuminate/Config/Repository.php:117
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
{
$value = $this->get($key, $default);
if (! is_int($value)) {
throw new InvalidArgumentException(
sprintf('Configuration value for key [%s] must be an integer, %s given.', $key, gettype($value))
);
}
return $value;
}
/**
* Get the specified float configuration value.
*
* @param string $key
* @param (\Closure():(float|null))|float|null $default
* @return float
*
* @throws \InvalidArgumentException
*/
public function float(string $key, $default = null): float
{View on GitHub (pinned to e0f6eb3518)