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 the supplied $items is a bare scalar (string, int, float, bool) — the match statement in from() has no arm for scalars and falls through to the default that throws. from() expects an iterable/array-like (array, Enumerable, Arrayable, WeakMap, Traversable, Jsonable, JsonSerializable, or object).

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 bd6b5437e6)

Solutions

  1. Use Arr::wrap($items) instead if you want a scalar promoted to a single-element array.
  2. Validate the input is iterable before calling Arr::from().
  3. Fix the producer to always pass an array/Collection.
  4. Branch on type: is_array($x) ? Arr::from($x) : Arr::from([$x]).

Example fix

// before
$items = Arr::from($maybeScalar);

// after
$items = Arr::wrap($maybeScalar);
// or
$items = Arr::from(is_iterable($maybeScalar) ? $maybeScalar : [$maybeScalar]);
Defensive patterns

Strategy: type-guard

Validate before calling

if (! is_iterable($items) && ! ($items instanceof \Illuminate\Contracts\Support\Arrayable) && ! is_object($items)) {
    $items = [$items]; // wrap scalars instead of calling Arr::from()
}

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling Arr::from('hello') or Arr::from(42); passing a scalar where an iterable is required.

Common situations: Loosely-typed code that sometimes receives a single scalar instead of a list; JSON decode returning a scalar (e.g. json_decode('"hi"')); a helper that wraps its argument but accidentally calls from() instead of wrap().

Related errors


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/0792aaf52d8984f2.json. Report an issue: GitHub.