Laravel 3 table Eloquent: 关系

Laravel 3 table Eloquent: relation

我有 3 table

角色

id `````` |```` `` 角色 ``````` |

1 `````` ```````` 员工

权限

id````````| `````````` 名字```````````` |

1 ````````````管理所有项目

2 ```````````` 管理客户

acces_permissions

id_permission FK | id_role FK
`````` `` 1 ``````````````````` 1

```````` 2 ``````````````````` 1

我将有一个 table 和

角色 |权限名称 |

工作人员 |管理所有项目管理客户 |

这 3 个型号怎么能这样

class roles extends Model
class permissions extends Model
class acces_permissions extends Model

如果我错了请纠正我,您想要实现的是使用用户角色获得所有权限..

最简单的方法是使用 belongsToMany eloquent 关系,在您的角色模型中添加此

public function permissions()
{
    return $this->belongsToMany('App\Permissions', 'access_permission_table', 'id_role', 'id_permission');
}

这样,当您调用 $role->permissions 时,您将获得该角色的所有权限。

我把

public function permissions()
{
    return $this->belongsToMany('permissions::class', 'access_permissions', 
    'id_role', 'id_permission');
}

in class 角色扩展模型

得到

$roles=roles::all();

在控制器中

@foreach($roles as $role)
$role['permissions']

在模板中

但没有工作

SQLSTATE[42S02]: Base table or view not found: 1146 Table 
'mycrm.access_permissions' doesn't exist (SQL: select `permissions`.*, 
`access_permissions`.`id_role` as `pivot_id_role`, 
`access_permissions`.`id_permission` as `pivot_id_permission` from 
`permissions` inner join `access_permissions` on `permissions`.`id` = 
`access_permissions`.`id_permission` where `access_permissions`.`id_role` = 
1) (View: 
F:\MYCRM\mycrm\packages\tc\calculator\src\views\permission_pack.blade.php)

但连接查询似乎是正确的

好的我做到了

public function permissions()
{
 return $this->belongsToMany(permissions::class, 'acces_permissions' , 'id_role', 
 'id_permission');
}

现在它的工作方式与以前不同,我不知道为什么