{"id":"5d4d014cee3f019c","repo":"laravel/framework","slug":"cache-value-for-key-s-must-be-an-integer-s-gi","errorCode":null,"errorMessage":"Cache value for key [%s] must be an integer, %s given.","messagePattern":"Cache value for key \\[(.+?)\\] must be an integer, (.+?) given\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Cache/Repository.php","lineNumber":280,"sourceCode":"     * @param  (\\Closure():(int|null))|int|null  $default\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function integer($key, $default = null): int\n    {\n        $key = enum_value($key);\n\n        $value = $this->get($key, $default);\n\n        if (is_int($value)) {\n            return $value;\n        }\n\n        if (filter_var($value, FILTER_VALIDATE_INT) !== false) {\n            return (int) $value;\n        }\n\n        throw new InvalidArgumentException(\n            sprintf('Cache value for key [%s] must be an integer, %s given.', $key, gettype($value))\n        );\n    }\n\n    /**\n     * Retrieve a float item from the cache.\n     *\n     * @param  \\UnitEnum|string  $key\n     * @param  (\\Closure():(float|null))|float|null  $default\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function float($key, $default = null): float\n    {\n        $key = enum_value($key);\n\n        $value = $this->get($key, $default);\n","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Cache/Repository.php#L262-L298","documentation":"Thrown by Cache\\Repository::integer() when the stored value is neither an int nor a numeric string that filter_var(FILTER_VALIDATE_INT) accepts. Unlike string(), integer() will coerce numeric strings, so this fires only for genuinely non-integer data (arrays, bools, non-numeric strings, null).","triggerScenarios":"Calling Cache::integer('key') when the stored value is an array, bool, object, the string 'abc', or null because the key is missing and no int default was passed.","commonSituations":"Storing a counter via Cache::put('count', ['n' => 1]) and reading it with integer(); a missing key combined with no default; a value cached as a float like '1.5' which FILTER_VALIDATE_INT rejects.","solutions":["Pass an int default: Cache::integer('key', 0).","Fix the writer to store a real int: Cache::put('key', (int) $value).","For float-derived counts use Cache::float() or cast before storing.","Clear the bad entry with Cache::forget('key')."],"exampleFix":"// before\n$count = Cache::integer('page.views');\n\n// after\n$count = Cache::integer('page.views', 0);\nCache::put('page.views', (int) $count, $ttl);","handlingStrategy":"type-guard","validationCode":"$value = Cache::get('page.views');\nif (! is_int($value) && filter_var($value, FILTER_VALIDATE_INT) === false) {\n    $value = 0;\n}","typeGuard":"function isCachedInt(string $key): bool {\n    $v = Cache::get($key);\n    return is_int($v) || filter_var($v, FILTER_VALIDATE_INT) !== false;\n}","tryCatchPattern":"try {\n    $count = Cache::integer('page.views', 0);\n} catch (\\InvalidArgumentException $e) {\n    $count = 0;\n    Cache::forget('page.views');\n}","preventionTips":["Pass an int default: Cache::integer('key', 0).","Store counters with (int) and use Cache::increment() for atomic ints.","Avoid caching floats under keys read by integer()."],"tags":["cache","type-mismatch","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}