Laravel - 如何从角色 table 中设置的另一个 table 获取权限行
Laravel - How to get rows of permissions from another table set in roles table
我是 laravel 的新手,正在尝试从 permissions
table 中获取设置到 roles
- permission
中的所有权限行字段作为 json
Obviously, this is not working as aspected. It returns an array of
collections. However, I want each permission rows that associate with
the roles results in itself.
public function index()
{
$roles = Role::all();
foreach ($roles as $role) {
$permissions[] = Permission::whereIn('id', json_decode($role->permission))->get();
}
dd($permissions);
}
权限table
角色table
建议将权限字段提取到单独的table。这将使实施约束以确保引用的权限 ID 和角色 ID 确实存在成为可能。可能的 table 名称将是:permission_role
,包含以下列:permission_id
和 role_id
.
迁移可以这样设置:
Schema::create('permission_role', function (Blueprint $table) {
$table->unsignedBigInteger('permission_id');
$table->unsignedBigInteger('role_id');
$table->foreign('permission_id')->references('id')->on('permissions');
$table->foreign('role_id')->references('id')->on('roles');
});
我是 laravel 的新手,正在尝试从 permissions
table 中获取设置到 roles
- permission
中的所有权限行字段作为 json
Obviously, this is not working as aspected. It returns an array of collections. However, I want each permission rows that associate with the roles results in itself.
public function index()
{
$roles = Role::all();
foreach ($roles as $role) {
$permissions[] = Permission::whereIn('id', json_decode($role->permission))->get();
}
dd($permissions);
}
权限table
角色table
建议将权限字段提取到单独的table。这将使实施约束以确保引用的权限 ID 和角色 ID 确实存在成为可能。可能的 table 名称将是:permission_role
,包含以下列:permission_id
和 role_id
.
迁移可以这样设置:
Schema::create('permission_role', function (Blueprint $table) {
$table->unsignedBigInteger('permission_id');
$table->unsignedBigInteger('role_id');
$table->foreign('permission_id')->references('id')->on('permissions');
$table->foreign('role_id')->references('id')->on('roles');
});