{"id":"22d3ba1f502e4347","repo":"laravel/framework","slug":"the-chunkbyid-operation-was-aborted-because-the","errorCode":null,"errorMessage":"The chunkById operation was aborted because the [{$alias}] column is not present in the query result.","messagePattern":"The chunkById operation was aborted because the \\[(.+?)\\] column is not present in the query result\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Concerns/BuildsQueries.php","lineNumber":214,"sourceCode":"            if ($countResults === 0) {\n                break;\n            }\n\n            if (! is_null($remaining)) {\n                $remaining = max($remaining - $countResults, 0);\n            }\n\n            // On each chunk result set, we will pass them to the callback and then let the\n            // developer take care of everything within the callback, which allows us to\n            // keep the memory low for spinning through large result sets for working.\n            if ($callback($results, $page) === false) {\n                return false;\n            }\n\n            $lastId = data_get($results->last(), $alias);\n\n            if ($lastId === null) {\n                throw new RuntimeException(\"The chunkById operation was aborted because the [{$alias}] column is not present in the query result.\");\n            }\n\n            unset($results);\n\n            $page++;\n        } while ($countResults == $count);\n\n        return true;\n    }\n\n    /**\n     * Execute a callback over each item while chunking by ID.\n     *\n     * @param  callable(TValue, int): mixed  $callback\n     * @param  int  $count\n     * @param  string|null  $column\n     * @param  string|null  $alias\n     * @return bool","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Concerns/BuildsQueries.php#L196-L232","documentation":"Thrown by orderedChunkById during chunkById/eachById when the alias column used to track pagination cursor ($lastId = data_get($results->last(), $alias)) is null — meaning the selected result rows do not expose that key. Without a valid lastId the next chunk would repeat the same rows forever, so Laravel aborts.","triggerScenarios":"Calling ->select('name')->chunkById(100, ...) without including the id column; using ->addSelect with a custom alias that does not match the defaultKeyName (usually 'id'); passing an $alias that does not correspond to a selected column; selecting aggregate/custom rows that drop the id.","commonSituations":"Optimising a query to select only specific columns and forgetting the id; using a custom primary key (uuid) without specifying $column/$alias; renaming the primary key column; Eloquent where get() returns objects without the expected attribute.","solutions":["Include the id (or your primary key) column in the select: ->select(['id','name'])->chunkById(...).","Pass the correct column and alias: ->chunkById(100, $cb, 'uuid', 'uuid').","Call ->select('*') or omit select entirely so the key column is present.","For Eloquent, ensure the model's getKeyName()/primaryKey matches the column you chunk on."],"exampleFix":"// before\nOrder::select('total')->chunkById(500, function ($orders) {\n    // ...\n});\n\n// after\nOrder::select(['id','total'])->chunkById(500, function ($orders) {\n    // ...\n});","handlingStrategy":"validation","validationCode":"$alias = $alias ?? (new $model)->getKeyName();\n$sample = $query->clone()->limit(1)->get([$alias]);\nif (! $sample->isEmpty() && $sample->first()->{$alias} === null) {\n    throw new \\RuntimeException(\"Column {$alias} not selected; chunkById would loop\");\n}","typeGuard":"function chunkColumnIsSelected(\\Illuminate\\Database\\Eloquent\\Builder $q, string $alias): bool\n{\n    $cols = $q->getQuery()->columns;\n    return $cols === null || in_array('*', $cols, true) || in_array($alias, $cols, true);\n}","tryCatchPattern":"try {\n    $query->chunkById(500, $cb, $column, $alias);\n} catch (\\RuntimeException $e) {\n    if (str_contains($e->getMessage(), 'chunkById operation was aborted')) {\n        $query->select('*')->chunkById(500, $cb, $column, $alias);\n    } else throw $e;\n}","preventionTips":["Always include the primary key column in select() when chunking.","Pass explicit column and alias matching your model's primary key.","Prefer chunkById over chunk for large tables to avoid offset issues."],"tags":["database","query-builder","chunking","pagination","eloquent"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}