使用Laravel 5.4,我有一個表隊和另一個表匹配 每場比賽都有隊主場ID和隊客場IDLaravel - 合併兩個關係給出了一個錯誤
我發現這在這種情況下合併以尋找球隊在主場或客場比賽中所有比賽的絕妙好處。但它似乎並不奏效。
/**
* 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()
}
我得到的錯誤是調用未定義功能合併 調用未定義的方法照亮\數據庫\查詢\生成器::合併()
請幫助 謝謝
..................
我甚至試過預先加載,在這種情況下,誤差變
關係方法必須返回類型照亮\數據庫的對象\ \關係
public function matches()
{
$matches = Team::with('matchesHome', 'matchesAway')->get();
return $matches;
}
可能重複[無法在Laravel的Eloquent集合上合併](https://stackoverflow.com/questions/33654225/cannot-merge-on-an-eloquent-collection-in-laravel) –