[英]Laravel 5.1 Fetch Rows between start_time and end_time (two different fields)
I've been working on reservation project, I collect request (time_start and time_end) and add 1:30 hour to each so i can prevent user to select conflicts. 我一直在做预订项目,我收集请求(time_start和time_end)并且每个都添加1:30小时,这样我就可以阻止用户选择冲突了。
I tried using "Between" already but it doesnt seems to work. 我尝试过使用“之间”但它似乎没有用。
->whereBetween('reservation_time_start', [$time_from, $time_to])
->whereBetween('reservation_time_end', [$time_from, $time_to])
I also tried this, 我也尝试过这个,
->whereRaw('((reservation_time_end <= ? AND reservation_time_end >= ?) OR (reservation_time_start >= ? AND reservation_time_start <= ?))',[$time_end, $time_start,$time_start, $time_end])
It work but in some cases like between some time range, i dont get any results. 它工作,但在某些情况下,如某些时间范围之间,我没有得到任何结果。 Like This.
像这样。
+----+------------------------+----------------------+
| id | reservation_time_start | reservation_time_end |
+----+------------------------+----------------------+
| 1 | 10:00:00 | 13:00:00 | <- i need to get this
| 2 | 12:00:00 | 14:00:00 |
| 3 | 14:00:00 | 15:00:00 |
+----+------------------------+----------------------+
sample user input's: 示例用户输入:
$reservation_time_start = 12:00:00 (-1:30 Hour)
$reservation_time_end = 14:00:00 (+ 1:30 hour)
and when i execute my code, it returns null. 当我执行我的代码时,它返回null。 Please help.
请帮忙。
You can use Carbon for your problem, 你可以使用Carbon来解决你的问题,
you have user input times as: 您的用户输入时间为:
$reservation_time_start = 12:00:00;
$reservation_time_end = 14:00:00;
I suggest you to use date and time, so your user input should be, 我建议你使用日期和时间,所以你的用户输入应该是,
$reservation_time_start = 2017-07-18 12:00:00;
$reservation_time_end = 2017-07-18 14:00:00;
Now, parse these times with Carbon. 现在,用Carbon解析这些时间。 After that add and substract 1:30 hour from end time and start time respectively.
之后,分别从结束时间和开始时间加1和减1分钟。
$start_time = Carbon::parse($reservation_time_start)->subMinutes(90);
$end_time = Carbon::parse($reservation_time_end)->addMinutes(90);
After, this you can use simple query like this, 之后,你可以使用这样的简单查询,
DB::table('reservation')->where(function($query)
use($start_time,$end_time){
$query->where('reservation_time_start','>',$start_time)
->where('reservation_time_start','<',$end_time);
})
->orWhere(function($query) use($start_time,$end_time){
$query->where('reservation_time_end','>',$start_time)
->where('reservation_time_end','<',$end_time);
})
->get();
From the above query you will get reservations in the given range of time. 从上面的查询中,您将在给定的时间范围内获得预订。
Hope you understand. 希望你能理解。
UPDATE UPDATE
In your case, You have to consider following cases in reservations. 在您的情况下,您必须在预订中考虑以下案例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.