laravel/framework · error · InvalidArgumentException

Array value for key [%s] must be a boolean, %s found.

Error message

Array value for key [%s] must be a boolean, %s found.

What it means

Thrown by Arr::boolean() when the value resolved via Arr::get() fails is_bool(). The accessor does not coerce 0/1/'true', so those values and null all trigger it.

Source

Thrown at src/Illuminate/Collections/Arr.php:105

            throw new InvalidArgumentException(
                sprintf('Array value for key [%s] must be an array, %s found.', $key, gettype($value))
            );
        }

        return $value;
    }

    /**
     * Get a boolean item from an array using "dot" notation.
     *
     * @throws \InvalidArgumentException
     */
    public static function boolean(ArrayAccess|array $array, string|int|null $key, ?bool $default = null): bool
    {
        $value = Arr::get($array, $key, $default);

        if (! is_bool($value)) {
            throw new InvalidArgumentException(
                sprintf('Array value for key [%s] must be a boolean, %s found.', $key, gettype($value))
            );
        }

        return $value;
    }

    /**
     * Collapse an array of arrays into a single array.
     *
     * @param  iterable  $array
     * @return array
     */
    public static function collapse($array)
    {
        $results = [];

        foreach ($array as $values) {

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Pass a bool default: Arr::boolean($data, 'enabled', false).
  2. Normalize the source: cast with filter_var($v, FILTER_VALIDATE_BOOLEAN) before storing/reading.
  3. Validate first: $v = Arr::get($data, 'enabled'); if (is_bool($v)) { ... }.
  4. Fix the dot path or schema so the key genuinely contains a bool.

Example fix

// before
$on = Arr::boolean($payload, 'features.beta');

// after
$on = Arr::boolean($payload, 'features.beta', false);
// or normalize
$raw = Arr::get($payload, 'features.beta');
$on = is_bool($raw) ? $raw : filter_var($raw, FILTER_VALIDATE_BOOLEAN);
Defensive patterns

Strategy: type-guard

Validate before calling

$value = Arr::get($data, 'enabled');
if (! is_bool($value)) {
    $value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
}

Type guard

function arrayKeyIsBool(array $array, string $key): bool {
    return is_bool(Arr::get($array, $key));
}

Try / catch

try {
    $on = Arr::boolean($data, 'enabled', false);
} catch (\InvalidArgumentException $e) {
    $on = false;
}

Prevention

When it happens

Trigger: Calling Arr::boolean($data, 'enabled') when the value is 1, 0, 'true', 'false', an array, or null (missing key, no bool default).

Common situations: Decoding JSON whose booleans came through as 'true'/'false' strings; reading a database/config value stored as tinyint(1) yielding 0/1; missing key with no default.

Related errors


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