{"id":"7582ad66d4370100","repo":"laravel/framework","slug":"step-value-must-be-at-least-1","errorCode":null,"errorMessage":"Step value must be at least 1.","messagePattern":"Step value must be at least 1\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Collections/Collection.php","lineNumber":961,"sourceCode":"     */\n    public function union($items)\n    {\n        return $this->newInstance($this->items + $this->getArrayableItems($items));\n    }\n\n    /**\n     * Create a new collection consisting of every n-th element.\n     *\n     * @param  int  $step\n     * @param  int  $offset\n     * @return ($step is positive-int ? static : never)\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function nth($step, $offset = 0)\n    {\n        if ($step < 1) {\n            throw new InvalidArgumentException('Step value must be at least 1.');\n        }\n\n        $new = [];\n\n        $position = 0;\n\n        foreach ($this->slice($offset)->items as $item) {\n            if ($position % $step === 0) {\n                $new[] = $item;\n            }\n\n            $position++;\n        }\n\n        return $this->newInstance($new);\n    }\n\n    /**","sourceCodeStart":943,"sourceCodeEnd":979,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Collections/Collection.php#L943-L979","documentation":"Thrown by Collection::nth($step) when $step < 1. nth() selects every $step-th element starting at an offset; a zero or negative step would loop forever or never advance, so it is rejected up front.","triggerScenarios":"Calling $collection->nth(0), nth(-1), or passing a computed step that resolves to 0/negative.","commonSituations":"Step derived from a division that can yield zero (e.g. intdiv(count, chunkSize) when chunkSize >= count); user-supplied input not validated; off-by-one in paging math.","solutions":["Clamp the step: $collection->nth(max(1, $step)).","Validate input before calling: if ($step >= 1) { ... }.","Use a fixed literal step when the intent is a known cadence (e.g. every 2nd).","Re-check the math that produced the step value."],"exampleFix":"// before\n$everyNth = $collection->nth($step);\n\n// after\n$everyNth = $collection->nth(max(1, (int) $step));","handlingStrategy":"validation","validationCode":"$step = max(1, (int) $step);\n$everyNth = $collection->nth($step);","typeGuard":"function isValidNthStep(int $step): bool {\n    return $step >= 1;\n}","tryCatchPattern":"try {\n    $everyNth = $collection->nth($step);\n} catch (\\InvalidArgumentException $e) {\n    $everyNth = $collection->nth(1);\n}","preventionTips":["Clamp step with max(1, $step).","Validate user-supplied step values.","Re-check division/intdiv math that derives a step."],"tags":["collections","validation","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}