我正在尝试获取数据库中每个客户端模型的约会模型小时的摘要。我希望看到客户总结的总数。我在某个地方出错了,因为我的代码目前总计各个约会的所有时间并返回。我希望客户能够看到它。这是代码:
$clientHours = Appointment::with('client')
->whereBetween('ends_at', [$from, $to])
->get();
$results= [];
foreach($clientHours as $collection)
{
$duration = [];
$date1 = $collection->starts_at;
$date2 = $collection->ends_at;
$start = Carbon::parse($date1);
$end = Carbon::parse($date2);
$length = $start->diffInMinutes($end)/60;
$duration[] = $length;
$totalHours = array_sum($duration);
$results [] = [
'name' => $collection->client->name
'totalHours' => $totalHours,
'charge' => $collection->client->contract_rate * $totalHours,
];
}
return view('admin')
答案 0 :(得分:0)
你可以在get()调用后对集合使用sum()方法。