简体   繁体   English

laravel雄辩的查询关系

[英]laravel eloquent query relationship

My table structure is as following: 我的表结构如下:

resumes 简历
-id -ID
-... -...

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

degrees
-id -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 但是我想要的是: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 还是有其他更好的方法来链接查询,以便我可以像这样$ resume-> degree那样直接从简历访问度表,我什至尝试过急切的加载方法,但无法通过链接来检索数据

I think it would be better to use belongsToMany . 我认为最好使用belongsToMany

Possibly something like this: 可能是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM