laravel/framework · error · InvalidArgumentException

Cache value for key [ ] must be a boolean, given.

Error message

Cache value for key [%s] must be a boolean, %s given.

What it means

Thrown by Repository::boolean() when the value is not strictly a bool. Unlike integer()/float(), there is NO filter_var coercion here, so strings '1'/'true'/'on', integer 1/0, and null all fail. Only actual true/false values pass.

Solutions

  1. Pass a bool default: Cache::boolean('key', false).
  2. Store actual booleans: Cache::put('flag', (bool) $flag).
  3. If you must accept '1'/'0'/'true', read with get() and cast manually.

Example fix

// before
$enabled = Cache::boolean('feature.x'); // stored as '1' throws
// after
$enabled = Cache::boolean('feature.x', false);
// or normalize at write time:
Cache::put('feature.x', (bool) $request->input('x'));
Defensive patterns

Strategy: validation

Validate before calling

// No coercion in boolean(); normalize input first.
$raw = Cache::get($key);
if ($raw !== null) {
    $raw = filter_var($raw, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
    if ($raw === null) { /* not boolean-like */ }
}
$val = Cache::boolean($key, false);

Type guard

function isCacheBooleanable(mixed $value): bool { return is_bool($value); }

Try / catch

try {
    $enabled = Cache::boolean('feature');
} catch (\InvalidArgumentException $e) {
    $enabled = false;
}

Prevention

When it happens

Trigger: Calling Cache::boolean('key') where the stored value is anything other than a literal PHP bool, including truthy strings, 0/1 ints, or null from a cache miss.

Common situations: Storing config flags as 1/0 or 'true' strings; migrating from get() with manual (bool) casts; cache miss without a bool default.

Related errors


AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11). Data as JSON: /api/errors/d600cfdce72165e8. Report an issue: GitHub.

Appendix: source

Thrown at src/Illuminate/Cache/Repository.php:327

        );
    }

    /**
     * Retrieve a boolean item from the cache.
     *
     * @param  \UnitEnum|string  $key
     * @param  (\Closure():(bool|null))|bool|null  $default
     *
     * @throws \InvalidArgumentException
     */
    public function boolean($key, $default = null): bool
    {
        $key = enum_value($key);

        $value = $this->get($key, $default);

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

        return $value;
    }

    /**
     * Retrieve an array item from the cache.
     *
     * @param  \UnitEnum|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($key, $default = null): array
    {

View on GitHub (pinned to e0f6eb3518)