laravel/framework · error · InvalidArgumentException

Cache value for key [ ] must be an array, given.

Error message

Cache value for key [%s] must be an array, %s given.

What it means

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.

Solutions

  1. Pass an array default: Cache::array('key', []).
  2. Store the plain array (->toArray() / ->all()) rather than an object.
  3. Verify the serialization driver handles nested arrays cleanly.

Example fix

// before
$list = Cache::array('items'); // throws if scalar stored
// after
$list = Cache::array('items', []);
Cache::put('items', $collection->all());
Defensive patterns

Strategy: validation

Validate before calling

$raw = Cache::get($key);
if (! is_array($raw)) {
    $raw = is_null($raw) ? [] : [$raw];
}
// or simply:
$val = Cache::array($key, []);

Type guard

function isCacheArrayable(mixed $value): bool { return is_array($value); }

Try / catch

try {
    $items = Cache::array('items');
} catch (\InvalidArgumentException $e) {
    $items = [];
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11). Data as JSON: /api/errors/ef56e9236acb78d4. Report an issue: GitHub.

Appendix: 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 e0f6eb3518)