Irfan Haque
Irfan Haque

Reputation: 23

laravel eloquent query relationship

My table structure is as following:

resumes
-id
-...

educations
-id
-resume_id
-degree_id
-...

degrees
-id
-name

My resume model is like this:

class Resume extends Eloquent {
  public function degree(){
    return $this->hasManyThrough('Degree', 'Education', 'resume_id', 'degree_id');
  }
}

The query formed is this:

select `education`.*, `degrees`.`resume_id` from `education` inner join `degrees` on`degrees`.`id` = `education`.`degree_id` where `degrees`.`resume_id` = 36035

But what I want is this : where education.resume_id = 36035

Or is there any other better way to chain the query so that I could directly access the degree table from resume like this $resume->degree I've even tried eager loading approach but didn't able to retrieve data by chaining

Upvotes: 1

Views: 208

Answers (1)

CharliePrynn
CharliePrynn

Reputation: 3082

I think it would be better to use belongsToMany.

Possibly something like this:

public function degree()
{
    return $this->belongsToMany('Degree')->withPivot('resume_id');
}

Upvotes: 1

Related Questions