lundi 27 mars 2017

Update in bulk - Laravel

I am trying to find someway to update multiple records at once like we have insert for creating multiple records at once.

Lets say I have an associated array which contains data to be updated in each row of a table employees:

$data = [
  ['id'=>1,'name'=>'xxx'],
  ['id'=>2,'name'=>'xxx'],
  ['id'=>3,'name'=>'xxx'],
  ['id'=>4,'name'=>'xxx']
];

One way of updating all these records is:

foreach($data as $d){
   Employee::where('id'=>$d['id'])->update('name'=>$d['name']);
}

where Employee is model for employees table ofocurse.

I am wondering if i can update all records by one line statement?

For instance if I had create new records from $data, I would use:

Employee::insert($data);

instead of looping through $data and use create() for each record.

Anything like that exists for updation as well?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire