Robert
Robert

Reputation: 147

Laravel 7 DB Query only where date range not in date range

I have something like this:

$from = min($start_date, $end_date);
$till = max($start_date, $end_date);

DB::table('booked')
     ->where('start', '<=', $from)
     ->where('end', '>=', $till)
     ->get();

This is a nice solution form stackoverflow.

It seems works, but I need the opposite.

In my DB I have this data: start: 2020-09-27 10:00:00 end: 2020-09-27 12:00:00 I have to query that rows where for example:

$start_date(2020-09-27 9:00:00) and $end_date(2020-09-27 11:00:00) where both not in start: and end: Hopefully my question is clear enough.

Upvotes: 0

Views: 137

Answers (1)

ahappydev
ahappydev

Reputation: 103

If I am getting you right this is what you need

$from = min($start_date, $end_date);
$till = max($start_date, $end_date);

DB::table('booked')
     ->where('start', '>=', $till)
     ->orWhere('end', '<=', $from)
     ->get();

Upvotes: 1

Related Questions