Using Laravel 5.4, I have a table Teams and another table Matches Each match has TEAM HOME ID and TEAM AWAY ID
I found this great trick to merge in such cases to find all the matches played by a team either at home or away. But it does not seem to work.
/**
* 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()
}
The error I am getting is Call to undefined function merge Call to undefined method Illuminate\Database\Query\Builder::merge()
Please help Thanks
..................
I have even tried eager loading, in that case the error becomes
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
public function matches()
{
$matches = Team::with('matchesHome', 'matchesAway')->get();
return $matches;
}