我正在创建一个简单的预订系统,我需要检查汽车是否可用。
我要创建一个自定义规则,但存在以下问题:
1.我需要将几个值从表单传递给pass方法:date,time_form,time_to,car_id。第一个和第二个问题很激动,因为;
2.如果必须在几个输入中使用它,我不确定如何在哪里调用该方法。
3.我想我需要创建一个查询来检查汽车是否存在。在纯PHP中,我创建查询,但是我不确定我的“ laravel”样式查询是否正确:
$result = Reservation::where(function ($query) use ($request) {
$query->whereBetween('time_from', [$request->input('time_from), $request->input('time_to')])
->orWhereBetween('time_to', [$request->input('time_from'), $request->input('time_to')]);
})->where(function ($query) use ($request) {$query
->whereBetween($request->input('time_from'), ['time_from','time_to'])
->orWhereBetween($request->input('time_to'), ['time_from','time_to']);
})
->where('date',$request->input('date'))
->where('car_id',$request->input('car_id'));
和纯PHP
$sql1 = "select * from reservation
where ('$time_from' BETWEEN time_from AND time_to or '$time_to' BETWEEN time_from AND time_to)
and (time_from BETWEEN '$time_from' AND '$time_to' or time_to BETWEEN '$time_from' AND '$time_to')
and car_id = '$car_id'";
我是laravel的新手,所以请您谅解。我正在寻求一些建议。也许是更好的方法。
答案 0 :(得分:0)
您可以尝试以下代码:
$result = Reservation::query()
->where(function ($query) use ($request) {
return $query
->whereBetween('time_from', [$request->input('time_from'), $request->input('time_to')])
->orWhereBetween('time_to', [$request->input('time_from'), $request->input('time_to')]);
})
->where(function ($query) use ($request) {
return $query
->whereRaw($request->input('time_from') . ' BETWEEN time_from AND time_to')
->orWhereRaw($request->input('time_to') . ' BETWEEN time_from AND time_to');
})
->where('date', $request->input('date'))
->where('car_id', $request->input('car_id'));