我需要这种逻辑的帮助,我希望用户在选择日期并申请表格时创建约会,但是如果用户在页面上花费大量时间,而其他人在他之前预订了确定日期,他需要查看错误并返回以选择一个日期。
这是我的查询
$result = Appointment::where('user_id', $post['user_id'])
->where('appointment_datetime', $post['bookingdate'] )
->whereBetween('appointment_time_start', [$begintime, $endtime])
->WhereBetween('appointment_time_end', [$begintime, $endtime])->get();
if ($result->isEmpty()) { /// Insert to database ///}
else { /// return back()->with('msg', 1); /// }
好像我想从12:00到13:00预约
我在数据库中有一个从11:00到13:00的约会,查询未检测到它。
如果我有12:00至14:00,则查询未检测到它。
有人可以告诉我我的查询出了什么问题,谢谢!!
答案 0 :(得分:1)
我的想法是,您可以在相关日期进行应用程序级别的约会。
首先,我更喜欢使用Carbon包装。如果没有,可以安装。
$appointments = Appointment::where('user_id', $post['user_id'])
->where('appointment_datetime', $post['bookingdate'])
->get();
$foundFlag = false;
if ($appointments->count() > 0) {
$beginDate = \Carbon\Carbon::create(date('Y-m-d') . ' ' . $begintime); // You can be add at the end of varible ":00" if not exists
$endDate = \Carbon\Carbon::create(date('Y-m-d') . ' ' . $endtime); //You can be add at the end of varible ":00" if not exists
foreach($appointments as $appointment){
$beginDateForCurrenAppointment = \Carbon\Carbon::create(date('Y-m-d') . ' ' . $appointment->appointment_time_start); // You can be add at the end of varible ":00" if not exists
$endDateForCurrenAppointment = \Carbon\Carbon::create(date('Y-m-d') . ' ' . $appointment->appointment_time_end); // You can be add at the end of varible ":00" if not exists
if ($beginDateForCurrenAppointment->between($beginDate, $endDate, true) || $endDateForCurrenAppointment->between($beginDate, $endDate, true) || $beginDate->between($beginDateForCurrenAppointment, $endDateForCurrenAppointment, true) || $endDate->between($beginDateForCurrenAppointment, $endDateForCurrenAppointment, true)) {
$foundFlag = true;
break;
}
}
}
if (! $foundFlag) {
// Insert Database
} else {
/// return back()->with('msg', 1); ///
}
if语句中的每个逻辑均表示:
如果两者之间的请求约会存在约会开始时间:
$beginDateForCurrenAppointment->between($beginDate, $endDate, true)
如果两者之间的请求约会存在约会结束时间:
$endDateForCurrenAppointment->between($beginDate, $endDate, true)
如果在请求的约会开始时间之间存在约会:
$beginDate->between($beginDateForCurrenAppointment, $endDateForCurrenAppointment, true)
如果在请求的约会结束时间之间存在约会:
$endDate->between($beginDateForCurrenAppointment, $endDateForCurrenAppointment, true)