user3653474
user3653474

Reputation: 3854

where not working with whereBetween in Laravel

I have this booking table

start_date | end_date | status

2020-12-15   2020-12-18    Active
2020-12-19   2020-12-22    Inactive

In this table booking data is getting saved,but i want to add where also with whereBetween to return those rows where status is Active. I have used below query but it is returning Inactive rows also.

 $available_start = date('Y-m-d', strtotime($request->start_date));
         $available_end = date('Y-m-d', strtotime($request->end_date));     
         $checkData =Booking::whereBetween('start_date', [
            $available_start, $available_end
        ])->orwhereBetween('end_date', [
            $available_start, $available_end
        ])->where('status','Active')->get();      

Any help is highly appreciated.

Upvotes: 0

Views: 117

Answers (1)

OMR
OMR

Reputation: 12188

you must merge the 'whereBetween' in one where to get the expected results:

 $available_start = date('Y-m-d', strtotime($request->start_date));
        $available_end = date('Y-m-d', strtotime($request->end_date));
        $checkData =Booking:: where(function ($query)use($available_start, $available_end){
            $query-> whereBetween('start_date', [
                $available_start, $available_end
            ])->orwhereBetween('end_date', [
                $available_start, $available_end]);
        })
        ->where('status','Active')->get();

Upvotes: 2

Related Questions