laravel/framework · error · InvalidArgumentException

Cache value for key [ ] must be a string, given.

Error message

Cache value for key [%s] must be a string, %s given.

What it means

Thrown by Repository::string() when the value fetched from the cache (or the resolved default) is not a PHP string. The method calls get($key, $default); if the key is absent and no default is supplied, get returns null, which fails is_string(). This is a hard type assertion with no coercion.

Solutions

  1. Pass a string default: Cache::string('key', '') so misses resolve to a string.
  2. Store the value as a string at write time (cast with (string) $value before Cache::put).
  3. Use Cache::get() if you intentionally accept mixed/null types.

Example fix

// before
$name = Cache::string('user.name'); // throws if missing
// after
$name = Cache::string('user.name', '');
Defensive patterns

Strategy: validation

Validate before calling

// Before calling Cache::string, ensure a string default is provided or check existence.
if (Cache::has($key)) {
    $val = Cache::get($key);
    if (! is_string($val)) {
        // normalize or skip
    }
}
// Simplest: always pass a default.
$val = Cache::string($key, '');

Type guard

function isCacheStringable(mixed $value): bool { return is_string($value); }

Try / catch

try {
    $name = Cache::string('user.name');
} catch (\InvalidArgumentException $e) {
    $name = '';
    logger()->warning('Cache string read failed', ['exception' => $e]);
}

Prevention

When it happens

Trigger: Calling Cache::string('key') (or the 'string' typed accessor) when the stored value is an int/bool/array/null, or when the key is missing and $default is omitted or resolves to a non-string.

Common situations: Migrating from get() to the typed string() accessor without supplying a default; storing numeric IDs as integers then reading them as strings; cache miss returning null.

Related errors


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

Appendix: source

Thrown at src/Illuminate/Cache/Repository.php:250

        });
    }

    /**
     * Retrieve a string item from the cache.
     *
     * @param  \UnitEnum|string  $key
     * @param  (\Closure():(string|null))|string|null  $default
     *
     * @throws \InvalidArgumentException
     */
    public function string($key, $default = null): string
    {
        $key = enum_value($key);

        $value = $this->get($key, $default);

        if (! is_string($value)) {
            throw new InvalidArgumentException(
                sprintf('Cache value for key [%s] must be a string, %s given.', $key, gettype($value))
            );
        }

        return $value;
    }

    /**
     * Retrieve an integer item from the cache.
     *
     * @param  \UnitEnum|string  $key
     * @param  (\Closure():(int|null))|int|null  $default
     *
     * @throws \InvalidArgumentException
     */
    public function integer($key, $default = null): int
    {
        $key = enum_value($key);

View on GitHub (pinned to e0f6eb3518)