遍历我试图通过集合进行迭代,并存储每个进行分组的记录(“类型”)。目前它将逐行存储所有单独的记录,但我需要的预期行为是存储组总数。laravel GROUPBY通过收集
DD()返回此。因此,我希望所有的约会时间为一组进行总结,然后存储组,.i.e测试= 5小时。 Test2 = 3小时。
Collection {#471 ▼
#items: array:2 [▼
"Test" => Collection {#460 ▼
#items: array:2 [▼
0 => Appointment {#463 ▶}
1 => Appointment {#464 ▶}
]
}
"Test2" => Collection {#450 ▼
#items: array:2 [▼
0 => Appointment {#465 ▶}
1 => Appointment {#466 ▶}
]
}
]
}
这里是控制器代码。
foreach($appointments as $appointment)
{
$duration = [];
$aType = $appointments->groupBy('type');
foreach($aType as $type)
{
$duration[] = $date1->diffInMinutes($date2);
}
$totalhours = array_sum($duration); //sum the hours
$Item = new AppItem;
$Item->type = $aType->type;
$Item->total_hours = $totalhours;
$Item->save();
}
像下面结束了工作。 –