Here's my Sample Table:
reservation_date | time_start | time_end 7/10/2017 | 12:00:00 | 14:00:00
what i'm trying to do is to return all reservations between time_start and time_end base on the request by the user. and sub/add 2 hours from time_start and time_end to prevent conflicts. I tried
$times_data = $this->getTimesData($request->reservation_time_start, $request->reservation_time_end);
$reservations = $reservations->whereBetween('reservation_time_start', $times_data)
protected function getTimesData($time_start, $time_end)
{
$time_from = $this->getTimeFrom($time_start);
$time_to = $this->getTimeTo($time_end);
return [$time_from, $time_to];
}
protected function getTimeFrom($time)
{
$time = Carbon::parse($time);
return $time->subHours(2)->toTimeString();
}
protected function getTimeTo($time)
{
$time = Carbon::parse($time);
return $time->addHours(2)->toTimeString();
}
But the result is not what i expected. i also tried
->whereRaw("(reservation_time_start BETWEEN ? AND ?) AND (reservation_time_end BETWEEN ? AND ?) ",$times_data)
but it returns null. Please help.
check the laravel documentation https://laravel.com/docs/5.4/queries I think you want to do something like that:
->whereBetween('reservation_time_start', [$time_from, $time_to])
->whereBetween('reservation_time_end', [$time_from, $time_to])
->get();
I found the solution to my problem:
i used whereRaw and i found out that when i used double quotes(inside whereRaw parameter), it returns null. then i change it to single quote then my problem solved. Heres my query,
$reservations->whereRaw('((reservation_time_start >= ? AND reservation_time_start <= ?) OR (reservation_time_end <= ? AND reservation_time_end >= ?))',[$time_start, $time_end, $time_end, $time_start])
->whereIn('reservation_status_id', [1, 2, 3])
->ofReservationDate($request->reservation_date);