I have data collection like this
illuminate\support\collection
items: array(3)
0: array(5)
user_id: '1'
mode: 'doci'
channel: 1
total: 1
addition: "[{"cash":0}]"
1: array(5)
user_id: '1'
mode: 'doci'
channel: 1
total: 1
addition: "[{"cash":0}]"
2: array(5)
user_id: '1'
mode: 'doci'
channel: 1
total: 1
addition: "[{"cash":10}]"
as you can see index 0 and 1 are same. And I wanna sum total of them because the data are same.
and I have trying this
$collection = collect([]);
$data->each(function ($item) use ($collection) {
$target = $collection->where('user_id', $item['user_id'])
->where('mode', $item['mode'])
->where('channel', $item['channel'])
->where('addition', $item['addition']);
if ($target->count() == 0) {
$collection->push($item);
} else {
$target->first()['total'] += $item['total'];
}
});
$data = $collection;
and resulting this
illuminate\support\collection
items: array(3)
0: array(5)
user_id: '1'
mode: 'doci'
channel: 1
total: 1 -> expected result is 2 but that still 1
addition: "[{"cash":0}]"
2: array(5)
user_id: '1'
mode: 'doci'
channel: 1
total: 1
addition: "[{"cash":10}]"
Expected result on index 0 and key total is 2, but existing resulting 1 What I missed?