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

afoysal's avatar

Relationship issue in Laravel

I have 3 tables applicants,skills and applicant_skill. Here applicant_skill is the pivot table. applicant_skill table has 2 columns applicant_id, skill_id. One Applicant has many Skills. One Skill has many Applicants.

I wrote below function in model Applicants.php

public function applicantSkill()
    {
        return $this->belongsToMany(ApplicantSkill::class);
    }

I wrote below function in model Skills.php

public function applicantSkill()
{
    return $this->belongsTo(Applicant_skill::class);
}

I wrote below function in model ApplicantSkill.php

public function Applicants()
{
    return $this->belongsTo(Applicants::class);
}

I am trying to fetch applicants in ApplicantController.php like below

$applicants = Applicants::with('applicant_skill', 'applicant_skill.skills')->paginate();

I am getting below error.

enter image description here

0 likes
2 replies
Mo7sin's avatar

You just need to call the relationship correctly!

$applicants = Applicants::with('applicantSkill', 'applicantSkill.skills')->paginate();
1 like
tykus's avatar
tykus
Best Answer
Level 104

Your relationships are incorrect, you do not have a relationship to the pivot, but to the other model; in fact, your pivot table doesn't need a model at all in your case:

// Applicants.php

public function skills()
{
    return $this->belongsToMany(Skills::class, 'applicant_skills');
}
// Skills.php

public function applicants()
{
    return $this->belongsToMany(Applicants::class, 'applicant_skills');
}

Then:

$applicants = Applicants::with('skills')->paginate();
1 like

Please or to participate in this conversation.