I have a project of Laravel with two tables
Variants
id | name | data (jsonb)
Devices
id | name | variants_id | data (jsonb)
Some of the values in Devices tables used from variant but it also has it's own values too.
So when we create a new device we use variant data and save it with devices along with it's own data.
Now when I want to update variant and only update those fields in all the devices without replacing non-matching values.
Is there a better way than running a foreach loop on all devices.
This is what I'm doing right now.
foreach($variant->devices as $device)
{
$data = $device->data;
foreach($variant->data as $key => $value)
{
$data[$key] = $value;
}
$device->data = $data;
$device->save();
}
This is the data in Variant
{"depth": 66.9, "width": 58.2, "height": 100.8, "pit_id": "1234", "variable": false, "liters_per_cm": 3.88194, "measurement_unit": "cm"}
This is the data in Devices
{"lat": "16.636192", "lng": "19.238912", "depth": 66.9, "width": 58.2, "height": 100.8, "pit_id": "1234", "total_liters": 391.299552, "liters_per_cm": 3.88194, "measurement_unit": "cm"}
I hope this makes sense and help will be highly appreciated