我有类User、User_Role和Role_权限以及对应表

users (id)

users_roles (user_id, role_id, one user_id has one role_id)

roles_permissions (role_id, permission_id, one role_id has many permission_id)

我想通过$user->权限为用户获取权限。为此我写了函数
public function permissions()
{
    return $this->hasManyThrough('User_Role', 'Role_Permission', 'user_id','role_id');
}

我明白错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles_permissions.user_id' in 'field list' (SQL: select `users_roles`.*, `roles_permissions`.`user_id` from `users_roles` inner join `roles_permissions` on `roles_permissions`.`id` = `users_roles`.`role_id` where `roles_permissions`.`user_id` = 1)

我做错什么了?谢谢!

最佳答案

切换前两个参数的顺序:

public function permissions()
{
    return $this->hasManyThrough('Role_Permission', 'User_Role', 'user_id', 'role_id');
}

Eloquent正在查找user_id表中的Role_Permission列,但找不到。。

10-07 21:19