To simplify the problem, as I am iterating through the array of objects returned by the eloquent query, when I attempt to change one of it's property it seems to be changing of all their properties.
Assuming fruits is the data returned from the eloquent query
var fruits =[{name:apple,nested_relationship:{price:$5},{name:pear,{price:$6},{name:grape}]
foreach ($fruits as fruit){
if($fruit->name == 'apple'){
$fruit->nested_relationship->color = 'red';
}
elseif($fruit->name == 'grape'){
$fruit->nested_relationship->color = 'green';
}
}
The final result seems to be [{name:'apple',nested_relationship:{price:$5,color:'green'}},{name:pear,nested_relationship:{price:$5,color:'green'}},},{name:'grape'}]
Can someone please tell me what is going on?