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

  1. Supply a string default: config()->string('app.locale', 'en').
  2. Ensure the underlying env var / config value is actually set and is a string.
  3. 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

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


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/42f858169739475e.json. Report an issue: GitHub.