如何在PHP/laravel中查询2次之间的时隙

如何在PHP/laravel中查询2次之间的时隙,php,laravel,Php,Laravel,我需要这方面的帮助,我希望用户在选择日期并申请表单时创建约会,但如果用户在页面上花费大量时间,并且其他人在他之前预订给定日期,他需要看到错误并返回选择日期 这是我的问题 $result = Appointment::where('user_id', $post['user_id']) ->where('appointment_datetime', $post['bookingdate'] ) ->whereBetween('appointment_time_start', [$

我需要这方面的帮助,我希望用户在选择日期并申请表单时创建约会,但如果用户在页面上花费大量时间,并且其他人在他之前预订给定日期,他需要看到错误并返回选择日期

这是我的问题

 $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,查询不会检测到它


谁能告诉我我的问题出在哪里,非常感谢

我的想法是,您可以在应用程序级别重复相关日期的预约时间

首先,我更喜欢使用碳包装。如果没有,可以安装

$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)
$beginDate->between($beginDateForCurrenAppointment, $endDateForCurrenAppointment, true)
如果请求约会存在于约会结束时间之间:

$endDateForCurrenAppointment->between($beginDate, $endDate, true)
$endDate->between($beginDateForCurrenAppointment, $endDateForCurrenAppointment, true)
如果在请求的约会开始时间之间存在约会:

$beginDateForCurrenAppointment->between($beginDate, $endDate, true)
$beginDate->between($beginDateForCurrenAppointment, $endDateForCurrenAppointment, true)
如果在请求的约会结束时间之间存在约会:

$endDateForCurrenAppointment->between($beginDate, $endDate, true)
$endDate->between($beginDateForCurrenAppointment, $endDateForCurrenAppointment, true)

预约时间有什么限制吗?或者可以?很好,梅苏蒂你帮我弄明白了!我想现在可以了再次谢谢你