首页 文章

在批量更新中获取行ID

提问于
浏览
2

我正在尝试优化下面提到的 foreach 循环

foreach($idsNoLongerInResponse as $offerId) {
  $offerId = $offerId->id;

  $offer = Offer::find($offerId);
  $offer->notes = "updated offer [offer_id=$offer->id]";

  $offer->save();
}

我已对此进行了优化,但我不确定如何在批量更新时获取每行的ID .

Offer::whereIn('id', $idsNoLongerInResponse)->update([
    'some_updates' => 1,

    // How can I get the row id here?
    'notes' => "updated offer [offer_id=$offer->id]"
]);

我走错了方向吗?任何帮助,将不胜感激 . 如果可能的话,请提供laravel eloquent和raw查询格式的解决方案 .

1 回答

  • 1

    您可以在更新中使用DB :: raw

    ...->update( array(
        'column' => DB::raw( 'column * 2' )
    ) );
    

    查看讨论here

    所以,按照你的例子,我认为你可以尝试这个解决方案

    Offer::whereIn('id', $idsNoLongerInResponse)->update([
        'some_updates' => 1,
    
        // How can I get the row id here?
        'notes' => DB::raw('CONCAT(CONCAT("updated offer [offer_id=", bot_offer_id), "]")')
    ]);
    

相关问题