laravel/framework · error · InvalidArgumentException

Configuration value for key [%s] must be an array, %s given.

Error message

Configuration value for key [%s] must be an array, %s given.

What it means

Thrown by Repository::array() (src/Illuminate/Config/Repository.php:183) when the resolved value is not an array. is_array() is strict, so comma-separated strings, JSON strings, null, or scalars fail.

Source

Thrown at src/Illuminate/Config/Repository.php:183

        return $value;
    }

    /**
     * Get the specified array configuration value.
     *
     * @param  string  $key
     * @param  (\Closure():(array<array-key, mixed>|null))|array<array-key, mixed>|null  $default
     * @return array<array-key, mixed>
     *
     * @throws \InvalidArgumentException
     */
    public function array(string $key, $default = null): array
    {
        $value = $this->get($key, $default);

        if (! is_array($value)) {
            throw new InvalidArgumentException(
                sprintf('Configuration value for key [%s] must be an array, %s given.', $key, gettype($value))
            );
        }

        return $value;
    }

    /**
     * Get the specified array configuration value as a collection.
     *
     * @param  string  $key
     * @param  (\Closure():(array<array-key, mixed>|null))|array<array-key, mixed>|null  $default
     * @return Collection<array-key, mixed>
     */
    public function collection(string $key, $default = null): Collection
    {
        return new Collection($this->array($key, $default));
    }

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Define the config value as an actual array in the config file: 'origins' => explode(',', env('CORS_ORIGINS', '')), or a literal array.
  2. Pass an array default at call site: config()->array('cors.origins', []).
  3. If the value is JSON, decode it in the config file: json_decode(env('X', '[]'), true).
  4. Confirm the config file is loaded and the key path is correct.

Example fix

// before
// 'webhooks' => env('STRIPE_WEBHOOKS'),  // string
config()->array('services.stripe.webhooks');

// after
// 'webhooks' => explode(',', env('STRIPE_WEBHOOKS', '')),
config()->array('services.stripe.webhooks', []);
Defensive patterns

Strategy: validation

Validate before calling

$v = config('cors.origins');
if (! is_array($v)) {
    // ensure config returns an array, decode JSON, or explode a CSV string
}

Type guard

function isArrayConfig(\Illuminate\Config\Repository $c, string $key): bool
{
    return is_array($c->get($key));
}

Try / catch

try {
    $origins = config()->array('cors.origins', []);
} catch (\InvalidArgumentException $e) {
    $origins = [];
}

Prevention

When it happens

Trigger: Calling config()->array('services.stripe.webhooks') when the stored value is a string, null (missing key with no default), or a comma-separated list. JSON-encoded values also fail because they are strings until decoded.

Common situations: Developers store lists as comma-separated env strings (e.g. CORS origins) but then access via array(). Also triggered when a config file omits a key entirely and no default is supplied.

Related errors


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