laravel/framework · error · InvalidArgumentException
Cache value for key [%s] must be a float, %s given.
Error message
Cache value for key [%s] must be a float, %s given.
What it means
Thrown by Cache\Repository::float() when the stored value is not a float and cannot be coerced via filter_var(FILTER_VALIDATE_FLOAT). Genuine ints are NOT auto-promoted by is_float() and filter_var on an int returns the int which is !== false, so ints are accepted; the error fires for arrays, bools, non-numeric strings, or null.
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 bd6b5437e6)
Solutions
- Pass a float default: Cache::float('rate', 0.0).
- Sanitize the writer: store the raw numeric value, Cache::put('rate', (float) $value).
- Strip non-numeric characters before storing if the source is a formatted string.
- Cache::forget('rate') to clear a malformed entry.
Example fix
// before
$rate = Cache::float('conversion.rate');
// after
$rate = Cache::float('conversion.rate', 0.0);
Cache::put('conversion.rate', (float) $raw, $ttl); Defensive patterns
Strategy: type-guard
Validate before calling
$value = Cache::get('rate');
if (! is_float($value) && filter_var($value, FILTER_VALIDATE_FLOAT) === false) {
$value = 0.0;
} Type guard
function isCachedFloat(string $key): bool {
$v = Cache::get($key);
return is_float($v) || filter_var($v, FILTER_VALIDATE_FLOAT) !== false;
} Try / catch
try {
$rate = Cache::float('rate', 0.0);
} catch (\InvalidArgumentException $e) {
$rate = 0.0;
} Prevention
- Store raw numeric values, not formatted strings like '3.14%'.
- Pass a float default: Cache::float('rate', 0.0).
- Validate the source before caching if it can be 'N/A' or empty.
When it happens
Trigger: Calling Cache::float('rate') when the stored value is an array, bool, the string 'n/a', or null because the key is absent and no float default was supplied.
Common situations: Caching a rate as a formatted string '3.14%' (the % breaks FILTER_VALIDATE_FLOAT) and reading with float(); missing key without default; storing an array of metrics under a scalar key.
Related errors
- Cache value for key [%s] must be a string, %s given.
- Cache value for key [%s] must be an integer, %s given.
- Cache value for key [%s] must be a boolean, %s given.
- Cache value for key [%s] must be an array, %s given.
- This cache store does not support locks.
AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06).
Data as JSON: /data/errors/abfe78749c398e91.json.
Report an issue: GitHub.