Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

oscarburgos's avatar

Relate three models with extra info

Hi all,

I´m updating myselft on Laravel 5 with a little project but I´m getting in an endless mess.

I´ve been looking for similar questions about my situation but I´ve haven´t found any good clue about this "usual" problem. All I´ve read is about two models with pivot table but what about three.

The scenario is next.

Three models: Season | Team | Player

I have create a forth model to relate then and to know how many games a player has played.

Profile:

  • season_id
  • team_id
  • player_id
  • games_played

In player model I define the relation: function played() { $this->hasMany('App\Profile','player_id','id); }

In the Profile model I define the relation: function player() { $this->belongsToMany('App\Player','player_id'); }

The question is, how should I do, or which additional relation should I define to show all the seasons with all the teams and the games played by a player?

Thanks.

0 likes
1 reply

Please or to participate in this conversation.