{"record":{"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/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Cache/Repository.php#L262-L298","documentation":"Thrown by Repository::integer() when the cached value is neither a real int nor a string that filter_var(FILTER_VALIDATE_INT) accepts. The method first tries is_int(), then attempts integer coercion, and only throws when both fail. Numeric strings like '42' pass; 'abc', floats like 3.14, bools, arrays, and null do not.","triggerScenarios":"Calling Cache::integer('key') where the stored value is a non-numeric string, a float, a bool, an array, or null (missing key with no default).","commonSituations":"Storing a float-formatted counter ('1.5') and reading as integer; cache miss without default; storing booleans and expecting 0/1 coercion (which does NOT happen here).","solutions":["Pass an int default: Cache::integer('key', 0).","Ensure the stored value is an int or an integer-string at write time.","Pre-coerce with (int) before Cache::put if the source is float/bool."],"exampleFix":"// before\n$views = Cache::integer('post.views'); // throws on miss\n// after\n$views = Cache::integer('post.views', 0);","handlingStrategy":"validation","validationCode":"$raw = Cache::get($key);\nif ($raw !== null && ! is_int($raw) && filter_var($raw, FILTER_VALIDATE_INT) === false) {\n    // not integer-coercible; normalize or abort\n}\n$val = Cache::integer($key, 0);","typeGuard":"function isCacheIntegerable(mixed $value): bool {\n    return is_int($value) || filter_var($value, FILTER_VALIDATE_INT) !== false;\n}","tryCatchPattern":"try {\n    $count = Cache::integer('visits');\n} catch (\\InvalidArgumentException $e) {\n    $count = 0;\n}","preventionTips":["Store counters as native ints, not strings.","Pass an int default to cover cache misses.","Avoid storing floats where an integer read is expected."],"tags":["cache","type-mismatch","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}