doudu4282 2014-01-23 13:08
浏览 130

如何使用eloquent进行批量数据库更新

I would like to improve the following code to insert 1000 records at once rather than within the chunked loop.

public function fire()
{
    $self = $this;
    $time = date('Y-m-d H:i:s');
    $this->info('Starting chunk: '.$time);

    Item::chunk(1000, function($items) use ($self)
    {
        foreach ($items as $item)
        {

            $sizeInBytes = $self->getCurlHeaders($item->bigimg);
            $item->imgsize = (string) $sizeInBytes;
            $item->save();
        }
        $time = date('Y-m-d H:i:s');
        $self->info('Ending chunk: '.$time);
        exit;
    });
}

How can I do that with the Eloquent ORM and Laravel? The code is running as an artisan task. The target table has ~1000000 rows.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置