2017-04-13 57 views
0

我對模型有以下查詢 - 但只查詢團隊匹配的條件僅適用於第一個where子句。關係使用參數分組查詢

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

如果我使用他們自己,他們正常工作 - 無論是返回只有一個項目,他們應該:

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

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

但串聯起來,只會給我所有的球隊,其中WBP匹配爲空,以及wbp!= null和is_played = false的任何團隊的所有匹配。

如何正確鏈接它?

回答

1

我需要鏈上的匹配項,其中()調用:

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