laravel/framework · error · InvalidArgumentException

Size value must be at least 1.

Error message

Size value must be at least 1.

What it means

Thrown by Collection::sliding($size, $step) when $size < 1. sliding() emits overlapping windows of length $size advancing by $step; a zero/negative window size is meaningless and is rejected before computing the window count.

Source

Thrown at src/Illuminate/Collections/Collection.php:1319

     */
    public function shuffle()
    {
        return $this->newInstance(Arr::shuffle($this->items));
    }

    /**
     * Create chunks representing a "sliding window" view of the items in the collection.
     *
     * @param  positive-int  $size
     * @param  positive-int  $step
     * @return static<int, static>
     *
     * @throws \InvalidArgumentException
     */
    public function sliding($size = 2, $step = 1)
    {
        if ($size < 1) {
            throw new InvalidArgumentException('Size value must be at least 1.');
        } elseif ($step < 1) {
            throw new InvalidArgumentException('Step value must be at least 1.');
        }

        $chunks = floor(($this->count() - $size) / $step) + 1;

        return static::times($chunks, fn ($number) => $this->slice(($number - 1) * $step, $size));
    }

    /**
     * Skip the first {$count} items.
     *
     * @param  int  $count
     * @return static
     */
    public function skip($count)
    {
        return $this->slice($count);

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Clamp the size: $collection->sliding(max(1, $size), $step).
  2. Validate before calling: if ($size >= 1 && $step >= 1) { ... }.
  3. Guard for short collections: skip sliding() when count() < $size.
  4. Re-check the math that produced the size.

Example fix

// before
$windows = $collection->sliding($size, $step);

// after
if ($collection->count() >= $size) {
    $windows = $collection->sliding(max(1, (int) $size), max(1, (int) $step));
} else {
    $windows = collect();
}
Defensive patterns

Strategy: validation

Validate before calling

if ($collection->count() >= 1 && $size >= 1 && $step >= 1) {
    $windows = $collection->sliding($size, $step);
} else {
    $windows = collect();
}

Type guard

function isValidSlidingSize(int $size): bool {
    return $size >= 1;
}

Try / catch

try {
    $windows = $collection->sliding($size, $step);
} catch (\InvalidArgumentException $e) {
    $windows = collect();
}

Prevention

When it happens

Trigger: Calling $collection->sliding(0), sliding(-1), or passing a computed size that resolves to 0/negative.

Common situations: Size derived from a smaller collection (e.g. sliding(count() - 1) on a 1-element collection yields 0); user-supplied window size; off-by-one in paging/window math.

Related errors


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