{"id":"d600cfdce72165e8","repo":"laravel/framework","slug":"cache-value-for-key-s-must-be-a-boolean-s-giv","errorCode":null,"errorMessage":"Cache value for key [%s] must be a boolean, %s given.","messagePattern":"Cache value for key \\[(.+?)\\] must be a boolean, (.+?) given\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Cache/Repository.php","lineNumber":327,"sourceCode":"        );\n    }\n\n    /**\n     * Retrieve a boolean item from the cache.\n     *\n     * @param  \\UnitEnum|string  $key\n     * @param  (\\Closure():(bool|null))|bool|null  $default\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function boolean($key, $default = null): bool\n    {\n        $key = enum_value($key);\n\n        $value = $this->get($key, $default);\n\n        if (! is_bool($value)) {\n            throw new InvalidArgumentException(\n                sprintf('Cache value for key [%s] must be a boolean, %s given.', $key, gettype($value))\n            );\n        }\n\n        return $value;\n    }\n\n    /**\n     * Retrieve an array item from the cache.\n     *\n     * @param  \\UnitEnum|string  $key\n     * @param  (\\Closure():(array<array-key, mixed>|null))|array<array-key, mixed>|null  $default\n     * @return array<array-key, mixed>\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function array($key, $default = null): array\n    {","sourceCodeStart":309,"sourceCodeEnd":345,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Cache/Repository.php#L309-L345","documentation":"Thrown by Cache\\Repository::boolean() when the stored value fails is_bool(). This accessor does no coercion, so the integers 0/1, the strings 'true'/'false', or null all fail. It exists to guarantee a real bool is returned.","triggerScenarios":"Calling Cache::boolean('feature.enabled') when the stored value is 1, 0, '1', 'true', an array, or null (missing key with no bool default).","commonSituations":"Caching a config flag from an env var that yields 'true'/1 instead of a real bool; storing enabled state as an int from a database column; missing key without a default.","solutions":["Pass a bool default: Cache::boolean('feature.enabled', false).","Cast at write time: Cache::put('feature.enabled', (bool) $flag, $ttl).","If the source only gives 0/1 or 'true'/'false', normalize before caching (filter_var($v, FILTER_VALIDATE_BOOL)).","Cache::forget('feature.enabled') after fixing the writer."],"exampleFix":"// before\n$on = Cache::boolean('feature.flag');\n\n// after\n$on = Cache::boolean('feature.flag', false);\nCache::put('feature.flag', (bool) filter_var($env, FILTER_VALIDATE_BOOLEAN), $ttl);","handlingStrategy":"type-guard","validationCode":"$value = Cache::get('feature.enabled');\nif (! is_bool($value)) {\n    // normalize common truthy/falsy representations\n    $value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;\n}","typeGuard":"function isCachedBool(string $key): bool {\n    return is_bool(Cache::get($key));\n}","tryCatchPattern":"try {\n    $on = Cache::boolean('feature.enabled', false);\n} catch (\\InvalidArgumentException $e) {\n    $on = false;\n}","preventionTips":["Always cache flags with (bool) or filter_var(FILTER_VALIDATE_BOOLEAN) at write time.","Pass a bool default: Cache::boolean('flag', false).","Do not store 'true'/'false' strings or 0/1 ints under keys read by boolean()."],"tags":["cache","type-mismatch","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}