【发布时间】:2018-01-15 17:31:12
【问题描述】:
使用 Laravel 5.4,我有一个表 Teams 和另一个表 Matches 每场比赛都有TEAM HOME ID和TEAM AWAY ID
我发现了一个很棒的技巧,可以在这种情况下合并以查找球队在主场或客场进行的所有比赛。但它似乎不起作用。
/**
* The matches that this team has played at home.
*/
public function matchesHome()
{
return $this->hasMany('App\Match', 'team_home_id', 'id');
}
/**
* The matches that this team has played away.
*/
public function matchesAway()
{
return $this->hasMany('App\Match', 'team_away_id', 'id');
}
/**
* The matches that this team has played.
*/
public function matches()
{
$matchesPlayedHome = $this->matchesHome();
$matchesPlayedAway = $this->matchesAway();
// Merge collections and return single collection.
return $matchesPlayedHome->merge($matchesPlayedAway); // cannot get this to work | Call to undefined method Illuminate\Database\Query\Builder::merge()
}
我得到的错误是 Call to undefined function merge 调用未定义的方法 Illuminate\Database\Query\Builder::merge()
请帮忙 谢谢
........
我什至尝试过预先加载,在这种情况下错误变成了
关系方法必须返回 Illuminate\Database\Eloquent\Relations\Relation 类型的对象
public function matches()
{
$matches = Team::with('matchesHome', 'matchesAway')->get();
return $matches;
}
【问题讨论】: