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
- Wrap scalars before passing: Arr::from([$value]) or Arr::from((array) $value).
- Validate with is_iterable()/is_array() upstream and normalize.
- 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
- Wrap unknown scalars in an array before Arr::from().
- Force JSON arrays with json_decode($json, true).
- Validate is_iterable()/is_array() at API boundaries.
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
- Array value for key [ ] must be a boolean, found.
- Array value for key [ ] must be a float, found.
- Array value for key [ ] must be a string, found.
- Array value for key [ ] must be an array, found.
- Array value for key [ ] must be an integer, found.
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)