Sizin yaptığınıza göre;
$appointments = DoctorAppointment::where('clinic_id',$id)->get();
// Tarihe göre gruplanmış iki adet Collection elde edeceksiniz:
$grouped = $appointments->groupBy('date');
@foreach($grouped as $day => $appointments)
<h2>{{ $day }}</h2>
<ul>
@foreach($appointments as $appointment)
<li>{{ $appointment->time}}</li>
@endforeach
</ul>
@endforeach
Bana sorarsanız;
Tabloda date ve time diye iki ayrı alan tutmayın. Ayrıca date, time gibi öntanımlı olan/olabilecek alan isimleri de kullanmaktan kaçının.
Onun yerine tek bir datetime alanı tutun. Örneğin
appointment_date. Bu durumda tablonuz şöyle oluyor:
+----+-----------+---------------------+
| id | clinic_id | appointment_date |
+----+-----------+---------------------+
| 1 | 1 | 2018-11-17 09:30:00 |
| 2 | 1 | 2018-11-17 10:00:00 |
| 3 | 1 | 2018-11-17 10:30:00 |
| 4 | 1 | 2018-11-17 11:00:00 |
| 5 | 1 | 2018-11-18 11:30:00 |
| 6 | 1 | 2018-11-18 13:00:00 |
| 7 | 1 | 2018-11-18 13:30:00 |
| 8 | 1 | 2018-11-18 14:00:00 |
+----+-----------+---------------------+
Daha sonra DoctorAppointment modelinize appointment_date alanının tarih olduğunu belirtin. Böylece otomatik Carbon objesine dönüştürülecek:
// app/DoctorAppointment.php
protected $dates = [
'appointment_date',
];
Şimdi bunu tarihe göre gruplayalım.
$appointments = DoctorAppointment::where('clinic_id',1)->get();
$grouped = $appointments->groupBy(function ($appointment, $key) {
// Burada sadece gün ay yıl kısmını alıyoruz. appointment_date alanını
// modelimizde tarih olarak belirttiğimiz için otomatik olarak Carbon
// objesi oluyor ve bu şekilde rahatça üzerinde oynayabiliyoruz.
return $appointment->appointment_date->format('d-m-Y');
});
@foreach($grouped as $day => $appointments)
<h2>{{ $day }}</h2>
<ul>
@foreach($appointments as $appointment)
<li>{{ $appointment->appointment_date->format('H:i') }}</li>
@endforeach
</ul>
@endforeach