Skip to content
Advertisement

Eloquent – Update eloquent relations with foreach loop

I have a controller function to send multiple devices for repair (essentially an update request against multiple existing devices_repairs records). When I try to retrieve the devices from the devices table alongside the repair details (Devices table, devices_repairs table), the records are retrieved but when I try to update the values the update request doesn’t save the new data.

This is my controller function:

JavaScript

When I do (dd($devices)) after changing the repair details in the loop, the attributes array has the new values, but again push() will not update the database even though no error is returned:

JavaScript

Here are my models. Devices Model:

JavaScript

DevicesRepairs model:

JavaScript

What am I doing wrong? I had this working with separate DB::Table update queries but I much prefer to use eloquent.

Advertisement

Answer

you need to push data inside your foreach loop

Instead of

JavaScript

Use this

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement