我想对 foreach 循环内的开始时间数据进行排序。
这是我的控制器代码:
private function getPatientList($search = null)
{
return Appointment::select('id', 'hospital_id', 'patient_id', 'serial_no', 'doctor_schedule_detail_id')
->whereHas('patient', function ($query) use ($search) {
$query->select('id', 'name', 'mobile_no', 'gender');
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('name_hash', hash('sha256', $search))
->orWhere('mobile_no_hash', hash('sha256', $search));
});
}
})->where('doctor_id', auth()->user()->doctor->id)
->where('hospital_id', auth()->user()->doctor->current_hospital_id)
->where('status', '!=', 'Cancelled')
->whereDate('reporting_time', date('Y-m-d'))
->orderBy('reporting_time')->get()
->groupBy('doctor_schedule_detail_id')->all();
}
型号中:
public function doctorScheduleDetail()
{
return $this->belongsTo(DoctorScheduleDetail::class);
}
预约有 doctor_schedule_detail_id 列以及与 doctor_schedule_details 表的关系。在 doctor_schedule_details 表中有 start_time 列。
我认为:
@foreach ($appointments as $doctorScheduleId => $appointmentsGroup)
<div class="row row-cards patient-list mb-3">
<h4 class="mb-0"><span class="">Slot {{ $loop->iteration }}</span> [{{ date("g:i A",
strtotime($appointmentsGroup[0]->doctorScheduleDetail->start_time)) }} - {{
date("g:i A", strtotime($appointmentsGroup[0]->doctorScheduleDetail->end_time)) }}]</h4>
@foreach ($appointmentsGroup as $appointment)
<div class="col-sm-6 col-lg-3">{{ $appointment?->patient?->name }}</div>
--------
@endforeach
</div>
@endforeach
在这里,我循环遍历分组数据。我想根据 $appointmentsGroup[0]->doctorScheduleDetail->start_time 进行排序 - 它位于第一个 foreach 中。
我该怎么做?
我试过了,但没用
private function getPatientList($search = null)
{
return Appointment::select('id', 'hospital_id', 'patient_id', 'serial_no', 'doctor_schedule_detail_id')
->whereHas('patient', function ($query) use ($search) {
$query->select('id', 'name', 'mobile_no', 'gender');
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('name_hash', hash('sha256', $search))
->orWhere('mobile_no_hash', hash('sha256', $search));
});
}
})->where('doctor_id', auth()->user()->doctor->id)
->where('hospital_id', auth()->user()->doctor->current_hospital_id)
->where('status', '!=', 'Cancelled')
->whereDate('reporting_time', date('Y-m-d'))
->orderBy('reporting_time')->get()
->groupBy('doctor_schedule_detail_id')
->map(function ($appointmentsGroup) {
return $appointmentsGroup->sortBy(function ($appointment) {
return strtotime($appointment->doctorScheduleDetail->start_time);
});
})
->all();
}
您已将排序应用于每个组内的约会,而不是对组本身进行排序。
因此,您可以做的是,分组后,根据与每个组中第一个约会关联的
doctorScheduleDetail
的 start_time 对组进行排序。
也渴望加载
doctorScheduleDetail
。
private function getPatientList($search = null)
{
$appointments = Appointment::with('doctorScheduleDetail')
->select('id', 'hospital_id', 'patient_id', 'serial_no', 'doctor_schedule_detail_id')
->whereHas('patient', function ($query) use ($search) {
$query->select('id', 'name', 'mobile_no', 'gender');
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('name_hash', hash('sha256', $search))
->orWhere('mobile_no_hash', hash('sha256', $search));
});
}
})
->where('doctor_id', auth()->user()->doctor->id)
->where('hospital_id', auth()->user()->doctor->current_hospital_id)
->where('status', '!=', 'Cancelled')
->whereDate('reporting_time', date('Y-m-d'))
->orderBy('reporting_time')
->get()
->groupBy('doctor_schedule_detail_id');
$sortedAppointments = $appointments->sortBy(function ($appointmentsGroup) {
return $appointmentsGroup->first()->doctorScheduleDetail->start_time;
});
return $sortedAppointments->all();
}