{"id":"e4117061240b1cfa","repo":"laravel/framework","slug":"the-chunk-size-should-be-at-least-1","errorCode":null,"errorMessage":"The chunk size should be at least 1","messagePattern":"The chunk size should be at least 1","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Concerns/BuildsQueries.php","lineNumber":256,"sourceCode":"                if ($callback($value, (($page - 1) * $count) + $key) === false) {\n                    return false;\n                }\n            }\n        }, $column, $alias);\n    }\n\n    /**\n     * Query lazily, by chunks of the given size.\n     *\n     * @param  int  $chunkSize\n     * @return \\Illuminate\\Support\\LazyCollection<int, TValue>\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function lazy($chunkSize = 1000)\n    {\n        if ($chunkSize < 1) {\n            throw new InvalidArgumentException('The chunk size should be at least 1');\n        }\n\n        $this->enforceOrderBy();\n\n        return new LazyCollection(function () use ($chunkSize) {\n            $page = 1;\n\n            while (true) {\n                $results = $this->forPage($page++, $chunkSize)->get();\n\n                foreach ($results as $result) {\n                    yield $result;\n                }\n\n                if ($results->count() < $chunkSize) {\n                    return;\n                }\n            }","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Concerns/BuildsQueries.php#L238-L274","documentation":"Thrown by Builder::lazy() when the $chunkSize argument is less than 1. lazy() streams results in pages of $chunkSize; a chunk size of 0 or negative would either never yield or loop incorrectly, so Laravel validates it up front with InvalidArgumentException.","triggerScenarios":"Calling ->lazy(0), ->lazy(-1), or ->lazy($configValue) where $configValue resolved to a non-positive integer. Common when chunk size is read from config/env and not validated.","commonSituations":"Config key like 'app.lazy_chunk_size' missing or empty; dynamic calculation that underflows to 0; copying a ->chunk() call where 0 was tolerated and converting to ->lazy().","solutions":["Pass a positive integer: ->lazy(500).","Validate/guard the size before calling: ->lazy(max(1, (int) config('app.chunk_size', 1000))).","Use the default by omitting the argument: ->lazy() defaults to 1000."],"exampleFix":"// before\nUser::lazy((int) config('app.chunk_size'));\n\n// after\nUser::lazy(max(1, (int) config('app.chunk_size', 1000)));","handlingStrategy":"validation","validationCode":"$size = max(1, (int) $size);\nModel::query()->lazy($size)->each(...);","typeGuard":"function isValidChunkSize(mixed $n): bool { return is_int($n) && $n >= 1; }","tryCatchPattern":null,"preventionTips":["Always coerce chunk sizes with max(1, ...).","Validate config-derived sizes at boot.","Default the parameter instead of passing 0."],"tags":["database","lazy-collection","chunking","validation"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}