php - laravel Eloquent 查询关系

标签 php mysql laravel orm eloquent

我的表结构如下:

简历
-id
-...

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

学位
-id
-姓名

我的简历模型是这样的:

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 直接从简历访问学位表 我什至尝试过预加载方法,但无法通过链接检索数据

最佳答案

我认为使用 belongsToMany 会更好。

可能是这样的:

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

关于php - laravel Eloquent 查询关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26861511/

相关文章:

javascript - 引用错误: bookshelf is not defined

mysql - 如何从 Laravel 迁移创建此过程

unit-testing - 调用模型的 Laravel 4 测试 Controller

php - 复制而不是更新

php - 无法从产品表中指定 'title' - php artisantinker

python - Django 说该表不存在,但它确实存在

mysql - 是否有很多 :through allow for individual edits to the same item?

Laravel 默认 Auth::routes() 在组前缀内

php - 在Swift 4中从PHP解析JSON

php - PHPExcel_Worksheet 绘图对象的高度和宽度应该使用什么单位?