Ad

How To Pass (start <= Date1 And End >= Date1) || (start <= Date2 And End >= Date2) Condition In Laravel

- 1 answer

Hi every one i want to pass condition like this: (start <= date1 and end >= date1) || (start <= date2 and end >= date2) in laravel. how to pass with this query?

$events = DB::table('christophheich_calendar_entries')->whereNull('deleted_at');

Any one can helpe ? thanks

Ad

Answer

You can use where statement like the following:

$events = DB::table('christophheich_calendar_entries')
    ->where(function($q) use($date1, $date2) {
        $q->where(function($q2) use($date1) {
            $q2->where('start', '<=', $date1)->where('end', '>=', $date1);
        })->orWhere(function($q2) use($date2) {
            $q2->where('start', '<=', $date2)->where('end', '>=', $date2);
        });
    })
    ->whereNull('deleted_at');

This is how to create a where clause with a inner query that handles the or part of the where. Make sure $date1 and $date2 are initialised before the query.

Ad
source: stackoverflow.com
Ad