samedi 11 janvier 2020

Laravel query not working when using as scope

I'm trying to find records by time interval. In table there are columns like 'date_from' and 'date_to', those are specify starts and ends date of an event.

public function scopeByTimeInterval($query, $dateInterval)
{
    $query->where(function ($query) use ($dateInterval) {
        [$from, $to] = $dateInterval;
        $query->where([
            ['date_from', '<=', $from],
            ['date_to', '>=', $to]
        ]);
        $query->orWhere([
            ['date_from', '>=', $from],
            ['date_to', '<=', $to]
        ]);
        $query->orWhereBetween('date_from', $dateInterval);
        $query->orWhereBetween('date_to', $dateInterval);
    });
}

when I use where query directly, there are no problem. I can see all events between those dates. But if I use it as a scope, it returns me every events in given year and month and not the interval..

What might cause kind of behavior ? Or am I missing something ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire