Laravel whereBetween with dates why not working?

Viewed 248

Here is my code:

$arr = Event::select('user_id', 'start', 'end')
    ->where('start', '!=', $from)
    ->whereBetween('start', [$from, $till])
    ->orWhere(function($query) use ($from, $till) {
        $query->where('end', '!=', $till)
            ->whereBetween('end', [$from, $till]);
    })->get();

start value: 2021-07-11 9:00:00 end value: 2021-07-11 15:00:00

$from value: 2021-07-11 10:30:00 $till value: 2021-07-11 13:00:00

I tried with Carbon as well, but the query result is nothing. What is the problem? Also I tried with:

$arr = Event::where('start', '>', $from)
    ->where('start', '<', $till)    
    ->get();

Simply not working!

1 Answers

The main code segment has some little flaw with

wherebetween('start', [$from, $till])

Because your start date doesn't lies between your from and till value. That returns nothing actually.

Try with your second solution. With a lil change in it.

$arr = Event::where('start', '<=', $from)
->where('end', '>=', $till)    
->get();
Related