I am trying to get the second lot of fields from a many to many relationship.
So, if, for example, I have this structure:
users
id
name
roles
id
role
user-roles
user_id
role_id
and I use the typical belongs to many syntax in the roles table:
return $this->belongsToMany('App\Models\Users', 'user-roles', 'user_id', 'role_id');
I will end up with something that looks like this:
[{
"id": 1,
"name": "Bruce banner",
"pivot": {
"user_id": 1,
"role_id": 1
}
}, {
"id": 2,
"name": "Steve Jobs",
"pivot": {
"user_id": 2,
"role_id": 2
}]
how do I get something that includes the actual roles names:
[{
"id": 1,
"name": "Bruce banner",
"pivot": {
"user_id": 1,
"role_id": 1,
"role": "admin"
}
}, {
"id": 2,
"name": "Steve Jobs",
"pivot": {
"user_id": 2,
"role_id": 2,
"role": "admin"
}]
I have Googled and most searches show how to include other fields from the pivot table using ->withPivot but that is not what I need.
Any help would be appreciated.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire