Relation Querying with parameter grouping

- 1 answer

Ad

I have the following query on a model - however the condition of querying only the teams matches applies solely to the first where clause.

$this->matches = $this->team->matches()->whereNull('wbp')->orWhere(function($q) {
        $q->whereNotNull('wbp')->where('is_played','=',0);
    })->get();

If I use them on their own, they are properly working - both returning exactly one Item as they should:

 $this->team->matches()->whereNull('wbp')->get();

 $this->team->matches()->where(function($q) {
        $q->whereNotNull('wbp')->where('is_played','=',0);
    })->get();

But chaining them will just give me all of the teams matches where wbp is null, as well as all matches of any team where wbp != null and is_played = false.

How do I properly chain it here?

Ad

Answer

Ad

I needed to chain the where on the matches() call:

$this->matches = $this->team->matches()->where(function ($q)
    {
        $q->whereNull('wbp')->orWhere(function($q) 
        {
            $q->whereNotNull('wbp')->where('is_played','=',0);
        });
    })->get();
Ad
source: stackoverflow.com
Ad