I'm creating a simple reservation system and I need to check that car is available. I'm gonna create a custom rule but I have following problems:
1. I need to pass an argument few values from form to passes method: date, time_form, time_to, car_id. The first and second question get excited because;
2. I'm not sure how and where to call that method in the controller if I have to use it in a few input's .
3. I suppose I need to create a query to check that car exist. In pure php I create query but I'm not sure that my "laravel" style query in god:
$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'));
and pure 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'";
I'm newbie in laravel so I am asking you for understanding. I'm asking for some advice. Maybe is some better way to do this.
You can try the code below:
$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'));