【发布时间】:2015-03-18 22:33:46
【问题描述】:
假设您有一个团队和一个比赛桌。该团队有多个匹配项,外键位于visitant_id 或local_id(另请参见https://github.com/laravel/framework/issues/1272)
在团队模型中:
public function allMatches()
{
return $this->hasMany('Match', 'visitant_id')->orWhere('local_id', $this->id);
}
这样可以正常工作:
$team = Team::find(2);
$matches = $team->all_matches;
此查询的结果:
select *
from `matches`
where `matches`.`visitant_id` = ?
or `local_id` = ?
但是,当使用额外的 where 子句进行扩展时,例如:
$matches = $team->all_matches->where('type','=',1);
查询变为
select *
from `matches`
where `matches`.`visitant_id` = ?
or `local_id` = ? and 'type' = ?
这意味着它会选择所有访问者匹配项,即使类型不正确,因为该子句周围没有 ()。有什么办法解决吗?
【问题讨论】:
标签: php laravel laravel-4 eloquent