laravel/framework · error · InvalidArgumentException

The chunk size should be at least 1

Error message

The chunk size should be at least 1

What it means

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.

Source

Thrown at src/Illuminate/Database/Concerns/BuildsQueries.php:256

                if ($callback($value, (($page - 1) * $count) + $key) === false) {
                    return false;
                }
            }
        }, $column, $alias);
    }

    /**
     * Query lazily, by chunks of the given size.
     *
     * @param  int  $chunkSize
     * @return \Illuminate\Support\LazyCollection<int, TValue>
     *
     * @throws \InvalidArgumentException
     */
    public function lazy($chunkSize = 1000)
    {
        if ($chunkSize < 1) {
            throw new InvalidArgumentException('The chunk size should be at least 1');
        }

        $this->enforceOrderBy();

        return new LazyCollection(function () use ($chunkSize) {
            $page = 1;

            while (true) {
                $results = $this->forPage($page++, $chunkSize)->get();

                foreach ($results as $result) {
                    yield $result;
                }

                if ($results->count() < $chunkSize) {
                    return;
                }
            }

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Pass a positive integer: ->lazy(500).
  2. Validate/guard the size before calling: ->lazy(max(1, (int) config('app.chunk_size', 1000))).
  3. Use the default by omitting the argument: ->lazy() defaults to 1000.

Example fix

// before
User::lazy((int) config('app.chunk_size'));

// after
User::lazy(max(1, (int) config('app.chunk_size', 1000)));
Defensive patterns

Strategy: validation

Validate before calling

$size = max(1, (int) $size);
Model::query()->lazy($size)->each(...);

Type guard

function isValidChunkSize(mixed $n): bool { return is_int($n) && $n >= 1; }

Prevention

When it happens

Trigger: 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.

Common situations: 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().

Related errors


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