filter the date of a future relationship

Add the same function you have on the whereHas method to the with method.

In the way you’re doing it, the whereHas is going to filter the Leagues that have a Fixture on that date, but the with is going to bring you all the Fixtures of each of those Leagues, not just those on the given date.

Another thing, for dates preferably use whereDate instead of just where.

That said, your query could look like this:

League::with(['fixtures' => function($q) use ($date) { 
        $q->whereDate('date', $date); 
    }])
    ->whereHas('fixtures', function($q) use ($date) { 
        $q->whereDate('date', $date); 
    })
    ->get();

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top