laravel/framework · error · InvalidArgumentException

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

Error message

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

What it means

Thrown by Repository::float() when the value is not a real float and cannot be coerced by filter_var(FILTER_VALIDATE_FLOAT). is_float() runs first; note that an integer (e.g. 5) is NOT a float in PHP, but filter_var will coerce integer-strings. Pure ints stored in cache will fail unless they are float-typed.

Solutions

  1. Pass a float default: Cache::float('key', 0.0).
  2. Store as float at write time: Cache::put('rate', (float) $rate).
  3. Cast integer sources to float before storing.

Example fix

// before
$rate = Cache::float('tax.rate'); // throws if stored int
// after
$rate = Cache::float('tax.rate', 0.0);
Defensive patterns

Strategy: validation

Validate before calling

$raw = Cache::get($key);
if ($raw !== null && ! is_float($raw) && filter_var($raw, FILTER_VALIDATE_FLOAT) === false) {
    // normalize
}
$val = Cache::float($key, 0.0);

Type guard

function isCacheFloatable(mixed $value): bool {
    return is_float($value) || filter_var($value, FILTER_VALIDATE_FLOAT) !== false;
}

Try / catch

try {
    $rate = Cache::float('rate');
} catch (\InvalidArgumentException $e) {
    $rate = 0.0;
}

Prevention

When it happens

Trigger: Calling Cache::float('key') with a stored int (not float), bool, array, object, non-numeric string, or null.

Common situations: Storing a price as an integer cents value and reading as float; cache miss without default; storing 'NaN' or empty strings.

Related errors


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

Appendix: source

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

     * @param  (\Closure():(float|null))|float|null  $default
     *
     * @throws \InvalidArgumentException
     */
    public function float($key, $default = null): float
    {
        $key = enum_value($key);

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

        if (is_float($value)) {
            return $value;
        }

        if (filter_var($value, FILTER_VALIDATE_FLOAT) !== false) {
            return (float) $value;
        }

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

    /**
     * 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);

View on GitHub (pinned to e0f6eb3518)