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. A sliding window of size 0 or negative is meaningless; the method guards size first, then step (see error 58). Default size is 2.

Solutions

  1. Clamp size: $collection->sliding(max(1, $size), $step).
  2. Validate $size >= 1 before calling.
  3. Use the default sliding() when a window of 2 is acceptable.

Example fix

// before
$windows = $col->sliding($windowSize); // throws if 0
// after
$windows = $col->sliding(max(1, (int) $windowSize), $step);
Defensive patterns

Strategy: validation

Validate before calling

$size = max(1, (int) $size);
$result = $collection->sliding($size, $step);

Type guard

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

Try / catch

try {
    $windows = $col->sliding($size, 1);
} catch (\InvalidArgumentException $e) {
    $windows = $col->sliding();
}

Prevention

When it happens

Trigger: Calling $collection->sliding(0) or ->sliding(-1, 1), or passing a computed size <= 0.

Common situations: Size derived from a configurable window that can be set to 0; mis-typed default in a wrapper function.

Related errors


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

Appendix: 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 e0f6eb3518)