简体   繁体   English

Laravel 5与`wherePivot`雄辩的查询`belongsToMany`无法正常工作

[英]Laravel 5 `belongsToMany` with `wherePivot` eloquent query is not working properly

Suppose I have users,courses and course_users tables, users belongs to many courses and the structure of pivot table course_users is as bellow: 假设我有users,coursescourse_users表,用户属于许多课程,数据透视表course_users的结构如下:

| | user_id | user_id | course_id | course_id | relation_type | 相关类型|
|-----------------------------------------------| | ----------------------------------------------- |

now I have bellow code: 现在我有下面的代码:

$user = User:find(1);

and I have bellow function in user model: 并且我在user模型中具有波纹管功能:

public function courses(){
    return $this->belongsToMany('App\Models\Course')->wherePivot('relation_type', 1)->orWherePivot('relation_type', 0)->withPivot('relation_type', 'created_at', 'updated_at');
}

and if I want to get the course of user: 如果我想了解用户的课程:

$user->courses();

But the output query of above function is like: 但是上面函数的输出查询是这样的:

select 
  `courses`.*, `course_user`.`user_id` as `pivot_user_id`,`course_user`.`course_id` as `pivot_course_id`, 
  `course_user`.`relation_type` as `pivot_relation_type`,`course_user`.`created_at` as `pivot_created_at`, `course_user`.`updated_at` as `pivot_updated_at` 
from 
  `courses` inner join `course_user` on `courses`.`id` = `course_user`.`course_id` 
where 
  `course_user`.`user_id` = '1' and `course_user`.`relation_type` = '0' or `course_user`.`relation_type` = '1';

But I need to have something like this: 但是我需要这样的东西:

select 
   `courses`.*, `course_user`.`user_id` as `pivot_user_id`, `course_user`.`course_id` as `pivot_course_id`, 
   `course_user`.`relation_type` as `pivot_relation_type`, `course_user`.`created_at` as `pivot_created_at`, `course_user`.`updated_at` as `pivot_updated_at` 
from 
   `courses` inner join `course_user` on `courses`.`id` = `course_user`.`course_id` 
where 
   `course_user`.`user_id` = '1' and (`course_user`.`relation_type` = '0' or `course_user`.`relation_type` = '1');

The difference is in where clause. 区别在于where子句。

The wherePivot method on the relationship is just a shortcut for the where method, but it takes care of adding on the pivot table qualifier for the constraints. 关系上的wherePivot方法只是where方法的快捷方式,但是它需要在数据透视表限定符上添加约束。 Because of this, it doesn't support closures, which is what you need to use in order to group your constraints. 因此,它不支持闭包,这是为了对约束进行分组所需要的。 Therefore, you will need to call the where method directly, and manually add in the table name qualifier to your where conditions. 因此,您将需要直接调用where方法,并手动将表名限定符添加到where条件。

Something like: 就像是:

public function courses() {
    return $this->belongsToMany('App\Models\Course')
        // pass a closure to group your constraints
        ->where(function ($query) {
            return $query->where('course_user.relation_type', 1)
                ->orWhere('course_user.relation_type', 0);
        })
        ->withPivot('relation_type', 'created_at', 'updated_at');
}

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

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