Level 80
@Naxon You won’t be able to do it with a built-in Eloquent relation, but you could create a method that returns a builder instance:
class Team extends Model
{
public function games()
{
return Game::where('home_team', '=', $this->id)->orWhere('away_team', '=', $this->id);
}
}
You can then use it like you would a relation:
$games = $team->games()->orderBy('date')->paginate();
Edit: Alternatively, you could add a scope on the Game model to return games for a given team:
class Game extends Model
{
public function forTeam(Builder $query, Team $team)
{
return $query->where(function ($query) use ($team) {
$query->where('home_team_id', '=', $team->getKey());
$query->orWhere('away_team_id', '=', $team->getKey());
});
}
}
$games = Game::forTeam($team)->orderBy('date')->paginate();
1 like