Laravel 的 chunkById 不明确列

Laravel's chunkById ambiguous column

当我在带有连接的查询生成器上使用 chunkById 时,出现以下错误:

SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference "id" is ambiguous

$query = \DB::table('table1')
            ->select([
                'table1.id'
            ])
            ->join('table2', 'table2.table1_id', '=', 'table1.id')
            ->orderBy('table1.id', 'DESC');

$query->chunkById(1000, function ($items) {
   //do something
});

它适用于第一个块,然后抛出错误。有什么方法可以指定 laravel 用来跟踪块的 ID 的 table 吗?

您还需要两个参数:

$query->chunkById(1000, function ($items) {
   //do something
}, 'table1.id', 'id');

阅读 Laravel 的 API 文档,了解有关第三列和第四列的更多详细信息:Laravel API doc