2017-04-13 76 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();