laravel/framework · error · InvalidArgumentException
Cache value for key [%s] must be an array, %s given.
Error message
Cache value for key [%s] must be an array, %s given.
What it means
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.
Source
Thrown at src/Illuminate/Cache/Repository.php:351
}
/**
* Retrieve an array item from the cache.
*
* @param \UnitEnum|string $key
* @param (\Closure():(array<array-key, mixed>|null))|array<array-key, mixed>|null $default
* @return array<array-key, mixed>
*
* @throws \InvalidArgumentException
*/
public function array($key, $default = null): array
{
$key = enum_value($key);
$value = $this->get($key, $default);
if (! is_array($value)) {
throw new InvalidArgumentException(
sprintf('Cache value for key [%s] must be an array, %s given.', $key, gettype($value))
);
}
return $value;
}
/**
* Store an item in the cache.
*
* @param \UnitEnum|array|string $key
* @param mixed $value
* @param \DateTimeInterface|\DateInterval|int|null $ttl
* @return bool
*/
public function put($key, $value, $ttl = null)
{
if (is_array($key)) {View on GitHub (pinned to bd6b5437e6)
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.
Example fix
// before
$items = Cache::array('cart.items');
// after
$items = Cache::array('cart.items', []);
Cache::put('cart.items', $cart->items()->toArray(), $ttl); Defensive patterns
Strategy: type-guard
Validate before calling
$value = Cache::get('items');
if (! is_array($value)) {
$value = [];
} Type guard
function isCachedArray(string $key): bool {
return is_array(Cache::get($key));
} Try / catch
try {
$items = Cache::array('items', []);
} catch (\InvalidArgumentException $e) {
$items = [];
Cache::forget('items');
} Prevention
- Cache the array, not its JSON-encoded string.
- Pass an array default: Cache::array('items', []).
- Use ->toArray() when caching Eloquent results.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cache value for key [%s] must be a string, %s given.
- Cache value for key [%s] must be an integer, %s given.
- Cache value for key [%s] must be a float, %s given.
- Cache value for key [%s] must be a boolean, %s given.
- This cache store does not support locks.
AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06).
Data as JSON: /data/errors/ef56e9236acb78d4.json.
Report an issue: GitHub.