I am using laravel 5.5, I have a Member model and a Team model, I made the belongsToMany relationship correctly. The problem is that in the pivot table of that relationship there are several data that I need to access, for example the fields seasonGames, seasonWins and personalRating, and I have not been able to access them, suddenly I'm doing things wrong, I tried adding the fields with the withPivot method but I can not rescue the data.
I did this method in the Member model:
public function teams(){
return $this->belongsToMany('App\Team','team_member','teamId','guid')->withPivot(['seasonGames', 'seasonWins', 'personalRating']);
}
but now what should I do to access that data? I tried using $ member-> pivot as the documentation shows but only contains the indexes of the tables and nothing else: /
EDITED: The tables and fields I use are these
members:
- guid PK
- name
teams:
- teamId PK
- name
- type
- rating
- seasonGames
- seasonWins
member_team:
- teamId (Team PK)
- guid (PK of members)
- seasonGames
- seasonWins
- personalRating
The models are:
class Member extends Model
{
protected $primaryKey = 'guid';
public function teams(){
return $this->belongsToMany('App\Team','team_member','teamId','guid')->withPivot(['seasonGames', 'seasonWins', 'personalRating']);
}
}
class Team extends Model
{
protected $primaryKey = "teamId";
public function members(){
return $this->belongsToMany('App\Member','team_member','teamId','guid');
}
}