2

I'm trying to optimize the below mentioned foreach loop

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

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

  $offer->save();
}

I have optimized that but I'm not sure how should I get the ID for each row while batch updating.

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

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

Am I going in the wrong direction? Any help would be appreciated. If possible please provide the solution in laravel eloquent and raw query format.

Suraj
  • 2,181
  • 2
  • 17
  • 25

1 Answers1

1

You can use a DB::raw in the update

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

Check discussion here

So, following your example, I think you can try this solution

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), "]")')
]);
Suraj
  • 2,181
  • 2
  • 17
  • 25
JérémyCasper
  • 182
  • 3
  • 17