我有一个用户表,它有一个 userRole 并且 userRole 属于 Role。所以,我也想获取 userRole 和 Role。
用户模型中的代码:
public function userRole()
{
return $this->hasOne(UserRole::class);
}
UserRole 模型中的代码:
public function role()
{
return $this->belongsTo('App\Role');
}
Controller 中的代码:
User::with('userRole', function ($role) {
$role->with(['Role']);
})
->wherehas('userRole', function ($query) {
$query->where('role_id','1');
})->get();
这给了我错误
"mb_strpos() expects parameter 1 to be string"
最佳答案
问题是,当您想要向 with()
方法添加约束时,您应该传递一个数组。
您的代码应该类似于:
User::with([
'userRole' => function ($query) {
...
}
])
...
关于php - 如何将已连接的表与 Laravel 中的其他表连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54840919/