I am having trouble defining the relationships in my Eloquent Models when using a 3 way intermediate pivot table.
Here are the 4 tables in my database:
users
- id
- name
instruments
- id
- key
instruments_users
- id
- user_id
- instrument_id
- level_id
levels
- id
- key
- Instruments keys can be: guitare, piano, trumpet, etc.
- Levels keys can be: beginner, intermediate, expert, etc.
Each user can play of 1 or more instrument. For each played instrument (relationship), we attribute a level with level_id
.
I am trying to get a list of instruments for a given user, with the corresponding level. Here is the JSON returned for a user with 1 instrument:
"instruments":[
{
"id":1,
"key":"guitar",
"pivot":{
"user_id":1,
"instrument_id":1,
"level_id":1
},
"level":[
{
"id":1,
"key":"beginner",
"pivot":{
"instrument_id":1,
"level_id":1
}
}
]
}
]
My issue here is that level
is an array, but I want only one level
attributed to each played instrument
. I believe this is because I used a belongsToMany
relationship, but that was the only way I found to be able to pass through the intermediate table (instruments_users
).
In Laravel I configured my Eloquent Models as follow:
User.php
public function instruments() {
return $this->belongsToMany(
Instrument::class,
'instruments_users'
)->withPivot('level_id');
}
Instrument.php
protected $with = ['level'];
public function users() {
return $this->belongsToMany(
User::class,
'instruments_users'
)->withPivot('level_id');
}
public function level() {
return $this->belongsToMany(
Level::class,
'instruments_users'
);
}
Level.php
public function instruments() {
return $this->belongsToMany(
Instrument::class,
'instruments_users'
);
}
user - level
and have each level be instrument specific and then each user will be associated to a level and to an instrument via that level – apokryfoslevels
table entirely and add the level to theinstruments_users
pivot table? – DigitalDrifterlevel
as an array with one element (because there can be only one element) instead of a single element. As for having instrument specific levels, that defeats the modularity of levels in my case. – Arnaud B