【问题标题】:How to connect already connected table with other table in laravel?如何将已经连接的表与laravel中的其他表连接起来?
【发布时间】:2019-07-17 08:44:55
【问题描述】:

我有一个用户表,它有一个用户角色,用户角色属于角色。所以,我也想获取 userRole 和 Role。

用户模型中的代码:

public function userRole()
{
    return $this->hasOne(UserRole::class);
}

UserRole 模型中的代码:

 public function role()
{
    return $this->belongsTo('App\Role');
}

控制器中的代码:

User::with('userRole', function ($role) {
  $role->with(['Role']);
})
->wherehas('userRole', function ($query) {
     $query->where('role_id','1');
 })->get();

这给了我错误

"mb_strpos() 期望参数 1 为字符串"

【问题讨论】:

    标签: php mysql laravel join


    【解决方案1】:

    问题是当你想给with()方法添加一个约束时你应该传递一个数组。

    您的代码应该类似于:

    User::with([
        'userRole' => function ($query) {
            ...
        }
    ])
    ...
    

    【讨论】:

      猜你喜欢
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 2023-01-15
      • 1970-01-01
      相关资源
      最近更新 更多