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 LazyCollection::sliding($size, $step) when $size is less than 1. sliding() creates a moving window of $size elements; a window size of 0 or negative has no meaning. The size guard runs before the step guard.

Source

Thrown at src/Illuminate/Collections/LazyCollection.php:1196

    #[\Override]
    public function shuffle()
    {
        return $this->passthru(__FUNCTION__, []);
    }

    /**
     * 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.');
        }

        return new static(function () use ($size, $step) {
            $iterator = $this->getIterator();

            $chunk = [];

            while ($iterator->valid()) {
                $chunk[$iterator->key()] = $iterator->current();

                if (count($chunk) == $size) {
                    yield (new static($chunk))->tap(function () use (&$chunk, $step) {
                        $chunk = array_slice($chunk, $step, null, true);
                    });

                    // If the $step between chunks is bigger than each chunk's $size

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Pass a size >= 1; clamp with max(1, $size).
  2. Default the source variable to a positive value (default arg is already 2).
  3. Guard the call when size may be 0.

Example fix

// before
$windows = $lazy->sliding($windowSize);

// after
$windows = $lazy->sliding(max(1, (int) $windowSize));
Defensive patterns

Strategy: validation

Validate before calling

$size = max(1, (int) $size);
$windows = $lazy->sliding($size, $step);

Type guard

function isPositiveWindowSize($size): bool { return is_int($size) && $size >= 1; }

Try / catch

try {
    $windows = $lazy->sliding($size, $step);
} catch (\InvalidArgumentException $e) {
    $windows = $lazy->sliding(2, max(1, $step));
}

Prevention

When it happens

Trigger: Calling $lazy->sliding(0) or $lazy->sliding(-1). Typically $size is computed from a config or input value that can be 0.

Common situations: Config-driven window sizes that default to 0 when unset. Computing window size from a length that may be 0.

Related errors


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