我正在尝试根据start(date)字段的计数向Laravel雄辩的集合添加额外的信息。因此,场景是我有一个“约会”表,我想向雄辩的返回的集合中的对象添加额外的属性(如果有一天有10个以上的约会)。我尝试了很多方法,例如分组和计数,但是没有用。无论如何,下面我有代码,我想我已经很接近了,但是我一直在努力,但是找不到如何使它实现我想要的东西。我想我被困住了,无法再想了。我评论了大部分内容。
$allAppointments = Appointment::get($columns); //returning all the appointments
$days = Appointment::get()->groupBy(function ($val) {
return Carbon::parse($val->start)->format('d');
}); //here I'm returning appointments by group without the count
$counter = [];
foreach ($days as $day => $appointments) {
foreach ($appointments as $appointment) {
if (Carbon::parse($appointment->start)->format('d') == $day) {
$counter = [Carbon::parse($appointment->start)->format('d') => $day]; //here I'm trying to make an array and count the days but not working
}
dd($counter);
foreach ($allAppointments as $allAppointment) {
if (Carbon::parse($allAppointment->start)->format('d') == $day && count($counter) == 10) //here I want to compare the dates and check if that day have more than 10 appointments
$allAppointment->setAttribute('backgroundColor', 'red'); //here I want to add the extra information
}
}
}
在工作了将近10个小时之后,这很奇怪,在这里发布它仅20分钟之后,我就可以使它工作了。代码如下:
public function all()
{
$columns = [
'id',
'title',
'start',
'end',
'note',
'allDay',
'editable',
];
$allAppointments = Appointment::get($columns);
$days = Appointment::get()
->groupBy(function ($val) {
return Carbon::parse($val->start)->format('d');
});
foreach ($days as $day => $appointments) {
foreach ($appointments as $appointment) {
foreach ($allAppointments as $key => $allAppointment) {
if (Carbon::parse($allAppointment->start)->format('d') == $day && $appointments->count() >= 10){
$allAppointment->setAttribute('backgroundColor', 'red');
}
}
}
}
return $allAppointments;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句