abu abu
abu abu

Reputation: 7028

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

Upvotes: 0

Views: 26

Answers (1)

Mayank Majithia
Mayank Majithia

Reputation: 1966

You have to use relationship as you have defined in models.

Model :- Applicants.php

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

you can use relationship in controller like,

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

You can refer doc here, Laravel-Eloquent-Relationships.

Upvotes: 1

Related Questions