laravel/framework · error · RuntimeException

The lazyById operation was aborted because the [{$alias}] co

Error message

The lazyById operation was aborted because the [{$alias}] column is not present in the query result.

What it means

Thrown inside orderedLazyById's LazyCollection generator when the alias column used as the cursor ($results->last()->{$alias}) is null on the last row of a chunk. Like the chunkById variant, this means the column is not present in the streamed rows and pagination cannot continue without looping.

Source

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

                $clone = clone $this;

                $results = match ($descending) {
                    SortDirection::Ascending, false => $clone->forPageAfterId($chunkSize, $lastId, $column)->get(),
                    SortDirection::Descending, true => $clone->forPageBeforeId($chunkSize, $lastId, $column)->get(),
                };

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

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

                $lastId = $results->last()->{$alias};

                if ($lastId === null) {
                    throw new RuntimeException("The lazyById operation was aborted because the [{$alias}] column is not present in the query result.");
                }
            }
        });
    }

    /**
     * Execute the query and get the first result.
     *
     * @param  array|string  $columns
     * @return TValue|null
     */
    public function first($columns = ['*'])
    {
        return $this->limit(1)->get($columns)->first();
    }

    /**
     * Execute the query and get the first result or throw an exception.

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Ensure the cursor column is selected: ->select(['id', ...other])->lazyById(500).
  2. Specify the correct column and alias: ->lazyById(500, 'uuid', 'uuid').
  3. Use ->select('*') to keep all columns, or add the primary key via addSelect.
  4. Confirm the model's getKeyName() returns the right column when relying on defaults.

Example fix

// before
Order::select('status')->lazyById(500)->each(...);

// after
Order::select(['id','status'])->lazyById(500)->each(...);
Defensive patterns

Strategy: validation

Validate before calling

$alias = $alias ?? (new $model)->getKeyName();
$probe = $query->clone()->limit(1)->get();
if ($probe->isNotEmpty() && ! array_key_exists($alias, (array) $probe->first())) {
    throw new \RuntimeException("Alias {$alias} missing for lazyById");
}

Type guard

function aliasPresentInSelect(\Illuminate\Database\Query\Builder $q, string $alias): bool
{
    $cols = $q->columns;
    return $cols === null || in_array('*', $cols, true) || in_array($alias, $cols, true);
}

Try / catch

try {
    $q->lazyById(500, $column, $alias)->each($cb);
} catch (\RuntimeException $e) {
    if (str_contains($e->getMessage(), 'lazyById operation was aborted')) {
        $q->select('*')->lazyById(500, $column, $alias)->each($cb);
    } else throw $e;
}

Prevention

When it happens

Trigger: Calling ->select('name')->lazyById(500) without the id column; passing an $alias that is not selected; selecting computed columns and dropping the primary key; Eloquent with a custom primary key where the alias defaults to 'id' but the column is 'uuid'.

Common situations: Column pruning that omits the cursor column; custom primary key without specifying $column; DB::raw or joins producing result rows without the expected attribute; using lazyById on a query with groupBy that drops the id.

Related errors


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