@brandymedia you can use, for example, https://laravel.com/docs/9.x/eloquent-resources to manipulate the data before returning
Oct 3, 2022
8
Level 4
access additional pivot table column
I implemented many to many relationship, not with two, but three tables. It works fine, but, I cant access pivot table in one of relationships. that's a problem because I need to get an extra column which is in the pivot table itself. here are my models
class Candidate extends Model
{
public function skills()
{
return $this->belongsToMany(Skill::class, 'candidate_criteria_score_skill');
}
}
class Skill extends Model
{
public function criterias()
{
return $this->hasMany(Criteria::class);
}
public function candidates()
{
return $this->belongsToMany(Candidate::class, 'candidate_criteria_score_skill');
}
}
class Criteria extends Model
{
public function skill()
{
return $this->belongsTo(Skill::class, 'candidate_criteria_score_skill');
}
}
When I run a query like this Candidate::with('skills.criterias')->find($id);
I get a response
{
"id": 1,
"name": "gela",
"skills": [
{
"id": 1,
"name": "Laravel",
"pivot": {
"candidate_id": 1,
"skill_id": 1
},
"criterias": [
{
"id": 1,
"name": "eloquent",
"skill_id": 1
},
{
"id": 2,
"name": "echo",
"skill_id": 1
}
]
},
{
"id": 2,
"name": "Node",
"pivot": {
"candidate_id": 1,
"skill_id": 2
},
"criterias": [
{
"id": 3,
"name": "async/await",
"skill_id": 2
},
{
"id": 4,
"name": "socket.io",
"skill_id": 2
}
]
}
]
}
pivot property is inside elements of the skills array, but I need it inside criterias. Is there a way to achieve that?
here is db schema
https://imgur.com/a/Guau23m
Please or to participate in this conversation.