{"record":{"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/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Cache/Repository.php#L333-L369","documentation":"Thrown by Repository::array() when the cached value is not a PHP array. Common after serialization round-trips that collapse to scalar, or when a key was overwritten with a scalar by another writer.","triggerScenarios":"Calling Cache::array('key') where the stored value is a scalar, object, null (miss with no default), or a serialized payload that did not deserialize back to an array.","commonSituations":"Storing a Collection or object that serializes to a non-array shape; cache miss without default; another process writing a scalar to the same key.","solutions":["Pass an array default: Cache::array('key', []).","Store the plain array (->toArray() / ->all()) rather than an object.","Verify the serialization driver handles nested arrays cleanly."],"exampleFix":"// before\n$list = Cache::array('items'); // throws if scalar stored\n// after\n$list = Cache::array('items', []);\nCache::put('items', $collection->all());","handlingStrategy":"validation","validationCode":"$raw = Cache::get($key);\nif (! is_array($raw)) {\n    $raw = is_null($raw) ? [] : [$raw];\n}\n// or simply:\n$val = Cache::array($key, []);","typeGuard":"function isCacheArrayable(mixed $value): bool { return is_array($value); }","tryCatchPattern":"try {\n    $items = Cache::array('items');\n} catch (\\InvalidArgumentException $e) {\n    $items = [];\n}","preventionTips":["Store ->toArray() / ->all() output, not objects.","Pass an array default to cover misses.","Verify nested structures deserialize as arrays (json_decode true)."],"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"}