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. A zero step would produce an infinite loop (the value never advances toward $to), so the guard prevents that. Note the check uses == so 0, 0.0, and '0' all trigger it.

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 bd6b5437e6)

Solutions

  1. Ensure $step is a non-zero number; guard with if ($step == 0) return collect().
  2. Clamp the step away from zero, or compute it only when bounds differ.
  3. Validate the source of $step before calling range().

Example fix

// before
$range = LazyCollection::range($start, $end, $end - $start);

// after
$step = $end - $start;
$range = $step == 0
    ? LazyCollection::range($start, $end, 1)
    : LazyCollection::range($start, $end, $step);
Defensive patterns

Strategy: validation

Validate before calling

if ($step == 0) {
    $range = \Illuminate\Support\LazyCollection::range($from, $to, 1);
} else {
    $range = \Illuminate\Support\LazyCollection::range($from, $to, $step);
}

Type guard

function isNonZeroStep($step): bool { return is_numeric($step) && $step != 0; }

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: Calling LazyCollection::range(1, 10, 0). Most often $step comes from a calculation or input that can yield 0, e.g. range(0, 100, $end - $start) when $end === $start.

Common situations: Dynamic step computed from user input or measurement deltas that can be 0. Defaulting $step from config that resolves to 0. Off-by-one in endpoint math producing equal bounds.

Related errors


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