update multi rows in laravel by eloquent

  database, eloquent, laravel, mysql, php

Suppose we have one dataBase that have one table called fathers; and another table called children.

I want get all children whose father is mamali.

$pls = children::where(['father_id' => 5, 'isGoodBoy' => true])->take(4)->get(); 

And i want change $pls and set father_id to 7,8,50,55. so can do this one by one request in foreach :

for ($i = 0; $i < count($pls); $i++) {
    $pls[$i] = $arayWhoWantBaby[$i];
    $pls[$i]->save();
}

This work but do with many request…(in this example 1 request for get and 4 request for update ! )

I want do this with one or two DB request,one to get data from DB and another set new data with one request to do all work and update items $pls[0][1][2]…

one thing as "in" keyword in sql for update ;

Source: Ask PHP

LEAVE A COMMENT