Pivot table values with laravel

1

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');
}
}
    
asked by kmilo93sd 05.03.2018 в 01:58
source

2 answers

0

To define a relation with the keys that you have in the different tables, you would have to use a good part of the arguments of belongsToMany .

These are these arguments:

public function belongsToMany(
    $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null,
        $parentKey = null, $relatedKey = null, $relation = null) 

Being:

  • $parentKey the key of the current model
  • $relatedKey the key of the related model

In the case of the Member model, it would be:

public function teams()
{
    return $this->belongsToMany('App\Team', 'member_team', 'guid', 'teamId', 'guid', 'teamId')
->withPivot(['seasonGames', 'seasonWins', 'personalRating']);
}

However, if you have the primary Key defined in the model taking into account that it is not 'id' , the last two arguments would not be necessary:

protected $primaryKey = 'guid';

Regarding the Team model, it would be something similar, but "inverted":

public function members()
{
    return $this->belongsToMany('App\Member', 'member_team', 'teamId', 'guid', 'teamId', 'guid')
->withPivot(['seasonGames', 'seasonWins', 'personalRating']);
}

And you can also define the custom key:

protected $primaryKey = 'teamId';
    
answered by 05.03.2018 / 03:47
source
0

I have been able to solve it, I was using the relationship backwards, I do not know if the answer is correct, but it has worked.

The Member model in its pivot attribute only contained the ids of the relationship, and for obvious reasons, I did not realize, the Team model only took those values by the way I defined the relationship method:

    public function members(){

    return $this->belongsToMany('App\Member','team_member','teamId','guid');

    }

I simply added the fields to the correct method in this way:

public function members(){

    return $this->belongsToMany('App\Member','team_member','teamId','guid')->withPivot(['seasonGames', 'seasonWins', 'personalRating']);

}

In this way, the model has been returned with the pivot next to the requested fields. To access them I only did the following in the Member model:

public function getSeastonGames(){

    return $this->pivot->seasonGames;
}

Thanks for the help guys

    
answered by 06.03.2018 в 19:36