Laravel Query builder / Scope question here meet requirements for multiple conditions in the same scope

  builder, laravel, mysql, scope

So I took over a project and now unfortunately the owners of my company want our entire search (the way this entire dang web app was built) to function differently. Currently we have scopes to only active charters and then some other conditions (which will stay). I now need to create an active scope that meets the 3 following requirement:

active = true and snooze_dates are null
active = true snooze_dates are not null but the $date passed in is not within the snooze_start/end
active = false and snooze_dates are not null and the $date passed in has a greater value than the snooze_end_date (charter will be activated at that point)

    public function scopeActiveWithSnooze($query, $date)
    $qOne = $query->where('active', true)
        ->where('snooze_start_date', null)
        ->where('snooze_end_date', null);
    //OR THIS
    $qTwo = $query->where('active', true)
        ->where('snooze_end_date', '!=', null)
        ->whereDate('snooze_end_date', '<=', $date)
        ->orWhereDate('snooze_start_date', '>=', $date);
    //OR THIS
    $qThree = $query->where('active', false)
        ->where('snooze_end_date', '!=', null)
        ->whereDate('snooze_end_date', '<=', $date);

this is the idea of the scope but how can I make all conditions work?

Source: Laravel

Leave a Reply