{"id":"ef56e9236acb78d4","repo":"laravel/framework","slug":"cache-value-for-key-s-must-be-an-array-s-give","errorCode":null,"errorMessage":"Cache value for key [%s] must be an array, %s given.","messagePattern":"Cache value for key \\[(.+?)\\] must be an array, (.+?) given\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Cache/Repository.php","lineNumber":351,"sourceCode":"    }\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    {\n        $key = enum_value($key);\n\n        $value = $this->get($key, $default);\n\n        if (! is_array($value)) {\n            throw new InvalidArgumentException(\n                sprintf('Cache value for key [%s] must be an array, %s given.', $key, gettype($value))\n            );\n        }\n\n        return $value;\n    }\n\n    /**\n     * Store an item in the cache.\n     *\n     * @param  \\UnitEnum|array|string  $key\n     * @param  mixed  $value\n     * @param  \\DateTimeInterface|\\DateInterval|int|null  $ttl\n     * @return bool\n     */\n    public function put($key, $value, $ttl = null)\n    {\n        if (is_array($key)) {","sourceCodeStart":333,"sourceCodeEnd":369,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Cache/Repository.php#L333-L369","documentation":"Thrown by Cache\\Repository::array() when the stored value fails is_array(). Common because serializers and some stores round-trip objects, and a missing key returns null. Use it to be sure you receive a true array.","triggerScenarios":"Calling Cache::array('items') when the stored value is an object, a scalar, the string '[]' (serialized JSON string rather than decoded array), or null from a missing key with no array default.","commonSituations":"Storing a model or stdClass instead of its ->toArray(); caching json_encode($data) (a string) then reading with array(); a stale key written before a schema change.","solutions":["Pass an array default: Cache::array('items', []).","Store the actual array: Cache::put('items', $model->toArray(), $ttl) or Cache::put('items', $data, $ttl) without json_encode.","If the store returns objects, cast/normalize: Cache::put('items', json_decode(json_encode($obj), true), $ttl).","Cache::forget('items') to drop a malformed entry."],"exampleFix":"// before\n$items = Cache::array('cart.items');\n\n// after\n$items = Cache::array('cart.items', []);\nCache::put('cart.items', $cart->items()->toArray(), $ttl);","handlingStrategy":"type-guard","validationCode":"$value = Cache::get('items');\nif (! is_array($value)) {\n    $value = [];\n}","typeGuard":"function isCachedArray(string $key): bool {\n    return is_array(Cache::get($key));\n}","tryCatchPattern":"try {\n    $items = Cache::array('items', []);\n} catch (\\InvalidArgumentException $e) {\n    $items = [];\n    Cache::forget('items');\n}","preventionTips":["Cache the array, not its JSON-encoded string.","Pass an array default: Cache::array('items', []).","Use ->toArray() when caching Eloquent results."],"tags":["cache","type-mismatch","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}