also i tried with
$doctor = User::where('name', $name)->with(['schedules', 'doctorDetail'])->firstOrFail();
$plans = Plan::get();
$latest_available_schedule = $doctor->schedules
->where('status', false)
->where('date', '>=', Carbon::today());
$plan = $plans->first();
$intervals = 1;
$reservation = [
'date' => "2024-02-21",
"start_time" => "10:00",
"plan_id" => 2, // just hours
];
$reservation_start_time = Carbon::parse($reservation['start_time']);
// Check if the reservation overlaps with existing reservations
foreach ($latest_available_schedule as $schedule) {
$startTime = Carbon::createFromFormat('H:i', $schedule->start_time);
$endTime = Carbon::createFromFormat('H:i', $schedule->end_time);
// Check if the reservation overlaps with this schedule
if ($reservation_start_time->between($startTime, $endTime)
|| $reservation_start_time->addHours($reservation['plan_id'])->between($startTime, $endTime)
|| $startTime->between($reservation_start_time, $reservation_start_time->addHours($reservation['plan_id']))
|| $endTime->between($reservation_start_time, $reservation_start_time->addHours($reservation['plan_id']))) {
// If there is an overlap, return with an error or handle accordingly
return "Overlap detected. Please choose another time slot.";
}
}
// If no overlap, proceed with creating the reservation
Schedule::create([
'doctor_id' => $doctor->id,
'date' => $reservation['date'],
'start_time' => $reservation_start_time->format('H:i'),
'end_time' => $reservation_start_time->addHours($reservation['plan_id'])->format('H:i'),
'status' => true,
]);
but doesn't work correctly