laravel/framework · error · InvalidArgumentException

Items cannot be represented by a scalar value.

Error message

Items cannot be represented by a scalar value.

What it means

Thrown by Arr::from() when $items is a scalar (int, float, bool, or plain string) that matches none of the match arms (array, Enumerable, Arrayable, WeakMap, Traversable, Jsonable, JsonSerializable, object). Scalars hit the default arm and throw.

Solutions

  1. Wrap scalars before passing: Arr::from([$value]) or Arr::from((array) $value).
  2. Validate with is_iterable()/is_array() upstream and normalize.
  3. Force array decoding: json_decode($json, true).

Example fix

// before
$arr = Arr::from($maybeScalar); // throws on 'foo'
// after
$arr = Arr::from(is_array($maybeScalar) ? $maybeScalar : [$maybeScalar]);
Defensive patterns

Strategy: type-guard

Validate before calling

if (is_array($items) || $items instanceof \Traversable || $items instanceof \Illuminate\Contracts\Support\Arrayable) {
    $arr = Arr::from($items);
} elseif (is_scalar($items)) {
    $arr = [$items];
} else {
    $arr = [];
}

Type guard

function isFromable(mixed $items): bool {
    return is_array($items)
        || $items instanceof \Traversable
        || $items instanceof \Illuminate\Contracts\Support\Arrayable
        || $items instanceof \Illuminate\Contracts\Support\Jsonable
        || $items instanceof \JsonSerializable
        || is_object($items);
}

Try / catch

try {
    $arr = Arr::from($maybeScalar);
} catch (\InvalidArgumentException $e) {
    $arr = is_scalar($maybeScalar) ? [$maybeScalar] : [];
}

Prevention

When it happens

Trigger: Calling Arr::from(42), Arr::from('hello'), Arr::from(true), or Arr::from(3.14). Also when a variable expected to be iterable holds a scalar.

Common situations: Passing user input that is sometimes a scalar string instead of an array; decoding JSON without forcing arrays; loose typing across API boundaries.

Related errors


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

Appendix: source

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

     * @template TValue = mixed
     *
     * @param  array<TKey, TValue>|Enumerable<TKey, TValue>|Arrayable<TKey, TValue>|WeakMap<object, TValue>|Traversable<TKey, TValue>|Jsonable|JsonSerializable|object  $items
     * @return ($items is WeakMap ? list<TValue> : array<TKey, TValue>)
     *
     * @throws \InvalidArgumentException
     */
    public static function from($items)
    {
        return match (true) {
            is_array($items) => $items,
            $items instanceof Enumerable => $items->all(),
            $items instanceof Arrayable => $items->toArray(),
            $items instanceof WeakMap => iterator_to_array($items, false),
            $items instanceof Traversable => iterator_to_array($items),
            $items instanceof Jsonable => json_decode($items->toJson(), true),
            $items instanceof JsonSerializable => (array) $items->jsonSerialize(),
            is_object($items) => (array) $items,
            default => throw new InvalidArgumentException('Items cannot be represented by a scalar value.'),
        };
    }

    /**
     * Get an item from an array using "dot" notation.
     *
     * @param  \ArrayAccess|array  $array
     * @param  string|int|null  $key
     * @param  mixed  $default
     * @return mixed
     */
    public static function get($array, $key, $default = null)
    {
        if (! static::accessible($array)) {
            return value($default);
        }

        if (is_null($key)) {

View on GitHub (pinned to e0f6eb3518)