Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DavidBuchukuri's avatar

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

0 likes
8 replies
DavidBuchukuri's avatar

@MohamedTammam I tried that, but it didn't work. You have to have pivot property first. Here pivot property is on skills not on criterias

SilenceBringer's avatar

@davidbuchukuri your Skill model hasMany Criteria, so, there are no pivot table between skills and criterias. Which pivot data you want to have?

DavidBuchukuri's avatar

@SilenceBringer score. each candidate has criteria and score, for example candidate A has criteria eloquent and he scored 18 points in that criteria. but criterias belong to skills, and at the end I want to get data in this format

{
		name: 'peter'
		skills: [
				{
					name: 'laravel',
					criterias: [
							{
								name: 'eloquent',
								score: 45
							}
						]
				},
				{
					name: 'vue js',
					criterias: [
							{
								name: 'pinia',
								score: 23
							}
						]
				}
		]

}
SilenceBringer's avatar

@DavidBuchukuri answered to you in another threa about it - do not think about the pivot as a pivot this way, make it separated model instead

Please or to participate in this conversation.