laravel雄辩的查询关系

时间:2014-11-11 09:26:43

标签: php mysql laravel orm eloquent

我的表格结构如下:

恢复
-id
-...

的教育
-id
-resume_id
-degree_id
-...


-id
-name

我的简历模型是这样的:

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

形成的查询是:

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

但我想要的是:where education.resume_id = 36035

或者还有其他更好的方法来链接查询,以便我可以直接从简历中访问学位表,如$ resume-> degree 我甚至尝试过急切的加载方式,但无法通过链接检索数据

1 个答案:

答案 0 :(得分:1)

我认为最好使用belongsToMany

可能是这样的:

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