laravel/framework · error · InvalidArgumentException

Array value for key [ ] must be a string, found.

Error message

Array value for key [%s] must be a string, %s found.

What it means

Thrown by Arr::string() when the resolved dot-notation value is not a string. Numeric/int/bool/null/array values all fail; this is a strict assertion with no coercion.

Solutions

  1. Pass a string default: Arr::string($data, 'key', '').
  2. Cast the source: (string) $data['name'] before lookup.
  3. Use Arr::get() and cast manually for mixed-type inputs.

Example fix

// before
$name = Arr::string($payload, 'name'); // int throws
// after
$name = Arr::string($payload, 'name', '');
Defensive patterns

Strategy: validation

Validate before calling

$raw = Arr::get($data, 'name');
if (! is_string($raw)) {
    $raw = is_null($raw) ? '' : (string) $raw;
}
// or pass a default.

Type guard

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

Try / catch

try {
    $name = Arr::string($payload, 'name');
} catch (\InvalidArgumentException $e) {
    $name = '';
}

Prevention

When it happens

Trigger: Calling Arr::string($data, 'key') where the value is an int, float, bool, null (miss), or array.

Common situations: JSON where a value is numeric rather than string; missing keys without default; IDs stored as integers.

Related errors


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

Appendix: source

Thrown at src/Illuminate/Collections/Arr.php:1193

     * @param  int-mask-of<SORT_REGULAR|SORT_NUMERIC|SORT_STRING|SORT_LOCALE_STRING|SORT_NATURAL|SORT_FLAG_CASE>  $options
     * @return array<TKey, TValue>
     */
    public static function sortRecursiveDesc($array, $options = SORT_REGULAR)
    {
        return static::sortRecursive($array, $options, SortDirection::Descending);
    }

    /**
     * Get a string item from an array using "dot" notation.
     *
     * @throws \InvalidArgumentException
     */
    public static function string(ArrayAccess|array $array, string|int|null $key, ?string $default = null): string
    {
        $value = Arr::get($array, $key, $default);

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

        return $value;
    }

    /**
     * Conditionally compile classes from an array into a CSS class list.
     *
     * @param  array<string, bool>|array<int, string|int>|string  $array
     * @return ($array is array<string, false> ? '' : ($array is '' ? '' : ($array is array{} ? '' : non-empty-string)))
     */
    public static function toCssClasses($array)
    {
        $classList = static::wrap($array);

        $classes = [];

View on GitHub (pinned to e0f6eb3518)