创建复杂的hasManyThrough Laravel 4查询

时间:2015-01-28 09:50:36

标签: php mysql laravel-4

我有User,User_Role和Role_Permission类以及对应的表

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)  

我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:1)

切换前两个参数的顺序:

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

Eloquent正在寻找user_id表中的Role_Permission列,但却找不到它。