如何验证跨时间Laravel



我的数据库中有类似的时间数据

+-------------+------------+------------+
|    id       | time_start | time_end   |
+---------------------------------------+
|    1        |  07:00     |  10:00     |
|    2        |  12:00     |  14:00     |
|             |            |            |
|             |            |            |
|             |            |            |
|             |            |            |
|             |            |            |
|             |            |            |
+-------------+------------+------------+

但我插入时间交叉,

time_start : 08:00
time_end : 09:00
or
time_start : 08:00
time_end : 11:00

如何验证time_starttime_end是否不在数据库中的日期之间?

我为您提供了在原始SQL中实现这一点的方法,您可以随意将其翻译成雄辩的繁文缛节:

SET @tstart='10:00';
SET @tend='11:00';
INSERT INTO t_time_range (time_start, time_end)
SELECT @tstart, @tend 
WHERE  NOT EXISTS
( SELECT 1 
FROM   t_time_range
WHERE  time_start >= @tstart AND time_start < @tend
OR     time_end > @tstart    AND time_end <= @tend
OR     @tstart >= time_start AND @tstart < time_end
);

假设您的模型是time,那么您要输入的时间是$time_start$end_time

$time = DB::table('time')
->where(['time_start', $time_start], 
['time_end', $end_time])
->first(); // see if there already is an entry

若$time并没有结果,请在数据库中输入时间。

最新更新