laravel/framework · error · RuntimeException

The lazyById operation was aborted because the

Error message

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

What it means

RuntimeException from orderedLazyById() (lazyById) when the alias/id column of the last row in a chunk is null - i.e. the column is not present on the result objects. Because lazyById streams by tracking the last id, a missing id makes continued paging unsafe and the operation aborts.

Solutions

  1. Include the id/alias column in the select: ->select(['id', 'name'])->lazyById(...).
  2. Pass $alias matching the actual returned attribute/column name.
  3. If you must select without the id, use lazy() instead and accept page-based behavior.
  4. For custom primary keys, set protected $primaryKey on the model and/or pass $column explicitly.

Example fix

// before
User::select('email')->lazyById(500)->each(fn ($u) => ...);
// after
User::select(['id', 'email'])->lazyById(500)->each(fn ($u) => ...);
Defensive patterns

Strategy: validation

Validate before calling

// Make sure the alias/id column is in the result before lazyById.
$alias = 'id';
$cols = $query->getQuery()->columns;
if ($cols !== null && ! in_array('*', $cols, true) && ! in_array($alias, $cols, true)) {
    $query->select(array_merge($cols, [$alias]));
}
$query->lazyById(500, column: $alias, alias: $alias)->each($callback);

Type guard

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

Try / catch

try {
    $query->lazyById($size)->each($callback);
} catch (\RuntimeException $e) {
    if (str_contains($e->getMessage(), 'column is not present in the query result')) {
        // add the alias/id column to select() and rerun
    }
    throw $e;
}

Prevention

When it happens

Trigger: Calling ->select('name')->lazyById(...) excluding the id column; passing a custom $alias not matching the selected column; selecting into arrays/DTOs where the id attribute name differs from the alias.

Common situations: Column pruning for performance that drops the primary key; aliasing the id in the query (e.g. ->select('id as identifier')) without passing $alias='identifier'; using lazyById with a model that maps columns to differently-named attributes.

Related errors


AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11). Data as JSON: /api/errors/048869f7adb82551. Report an issue: GitHub.

Appendix: 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 e0f6eb3518)