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

  1. Cast the env value in the config file: env('CACHE_TTL', 300) with an int default, or (int) env(...).
  2. Provide an int default: Config::integer('cache.ttl', 300).
  3. 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

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


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)