laravel/framework · error · InvalidArgumentException

You requested {$requested} items, but there are only {$count

Error message

You requested {$requested} items, but there are only {$count} items available.

What it means

Thrown by Arr::random() when the requested $number exceeds count($array). The method checks the requested count against the array size before drawing random keys, so it fails fast rather than returning fewer items than asked for.

Source

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

    /**
     * Get one or a specified number of random values from an array.
     *
     * @param  array  $array
     * @param  int|null  $number
     * @param  bool  $preserveKeys
     * @return ($number is null ? mixed : array)
     *
     * @throws \InvalidArgumentException
     */
    public static function random($array, $number = null, $preserveKeys = false)
    {
        $requested = is_null($number) ? 1 : $number;

        $count = count($array);

        if ($requested > $count) {
            throw new InvalidArgumentException(
                "You requested {$requested} items, but there are only {$count} items available."
            );
        }

        if (empty($array) || (! is_null($number) && $number <= 0)) {
            return is_null($number) ? null : [];
        }

        $keys = (new Randomizer)->pickArrayKeys($array, $requested);

        if (is_null($number)) {
            return $array[$keys[0]];
        }

        $results = [];

        if ($preserveKeys) {
            foreach ($keys as $key) {

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Clamp the count to the array size: Arr::random($arr, min($n, count($arr))).
  2. Validate/guard before calling: if ($n <= count($arr)) { ... }.
  3. Use Arr::random($arr) (no count) when you only need a single item.
  4. Handle empty input explicitly before random() since empty arrays short-circuit.

Example fix

// before
$picks = Arr::random($pool, $requested);

// after
$picks = Arr::random($pool, min($requested, count($pool)));
// or guard
if (count($pool) === 0) { return []; }
$picks = Arr::random($pool, min($requested, count($pool)));
Defensive patterns

Strategy: validation

Validate before calling

$count = count($pool);
$request = min($requested, $count);
if ($count === 0 || $request <= 0) {
    return [];
}
$picks = Arr::random($pool, $request);

Type guard

function canRandom(array $array, int $number): bool {
    return $number >= 1 && $number <= count($array);
}

Try / catch

try {
    $picks = Arr::random($pool, $requested);
} catch (\InvalidArgumentException $e) {
    $picks = Arr::random($pool); // fall back to a single item
}

Prevention

When it happens

Trigger: Calling Arr::random([1,2,3], 5); or $collection->random(count() + 1).

Common situations: Dynamic count derived from user input or another collection's size without clamping; calling random() on an empty or short list; off-by-one when passing total() instead of total()-1.

Related errors


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