dthp96899 2016-01-31 21:26
浏览 5

Laravel多对多关系回归意外结果

I have problems with my many to many relationship returned data. I have 3 tables to define this relationship users, institutes and the pivot table users_institutes. The data in my users_institutes is in the image.

enter image description here

My relationship is defined by the following code.

public function super_user_institutes()
{
    return $this->belongsToMany('App\Institute', 'users_institutes')
        ->wherePivot('role', 'inst_superuser')
        ->orWherePivot('role', 'inst_admin')
        ->orWherePivot('role', 'inst_staff')
        ->withPivot('role');
}

Now, for the user I'm trying to get the relations for has the id 2. So, now if I use

$user->super_user_institutes;

I get the following rows in response: 1, 2, 3, 9, 10, 11, 12 (7 rows).

Where I expect the following rows: 1, 2, 3.

Am I expecting a wrong result? or my relationship definition is wrong?

  • 写回答

2条回答 默认 最新

  • douao1858 2016-02-02 08:28
    关注

    try this much code only

    public function super_user_institutes()
       {
          return $this->belongsToMany('App\Institute', 'users_institutes');
       }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错