laravel/framework · error · InvalidArgumentException

Step value cannot be zero.

Error message

Step value cannot be zero.

What it means

Thrown by LazyCollection::range($from, $to, $step) when $step equals 0, which would produce an infinite loop. The method generates a sequence and increments/decrements by abs($step), so zero is explicitly rejected with an InvalidArgumentException. The conditional return type marks the result as `never` when step is 0.

Solutions

  1. Pass a non-zero step (typically >= 1): LazyCollection::range($from, $to, max(1, $step)).
  2. Guard the step before calling: if ($step == 0) { return []; }.
  3. Validate the step value at the source (config/request) and reject zero.

Example fix

// before
$range = LazyCollection::range($min, $max, $stride);
// after
$range = $stride > 0
    ? LazyCollection::range($min, $max, $stride)
    : LazyCollection::range($min, $max, 1);
Defensive patterns

Strategy: validation

Validate before calling

if ($step == 0) {
    throw new \InvalidArgumentException('step must be non-zero');
}
$range = \Illuminate\Support\LazyCollection::range($from, $to, $step);

Type guard

$step = (is_numeric($step) && $step != 0) ? (int) $step : 1;

Try / catch

try {
    $range = \Illuminate\Support\LazyCollection::range($from, $to, $step);
} catch (\InvalidArgumentException $e) {
    $range = \Illuminate\Support\LazyCollection::range($from, $to, 1);
}

Prevention

When it happens

Trigger: LazyCollection::range(1, 10, 0); computing $step from a division or config value that resolves to 0; default-arg overrides that pass 0.

Common situations: Step derived from a delta (to - from) that is 0; a 'stride' config defaulting to 0; reusing a step variable that was initialised to 0 and never set.

Related errors


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

Appendix: source

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

    public static function make($items = [], ...$args)
    {
        return new static($items, ...$args);
    }

    /**
     * Create a collection with the given range.
     *
     * @param  int  $from
     * @param  int  $to
     * @param  int  $step
     * @return ($step is 0 ? never : static<int, int>)
     *
     * @throws \InvalidArgumentException
     */
    public static function range($from, $to, $step = 1, ...$args)
    {
        if ($step == 0) {
            throw new InvalidArgumentException('Step value cannot be zero.');
        }

        return new static(function () use ($from, $to, $step) {
            if ($from <= $to) {
                for (; $from <= $to; $from += abs($step)) {
                    yield $from;
                }
            } else {
                for (; $from >= $to; $from -= abs($step)) {
                    yield $from;
                }
            }
        });
    }

    /**
     * Get all items in the enumerable.
     *

View on GitHub (pinned to e0f6eb3518)