where('role_id', $roleId)->delete(); // 批量插入新角色资源关联 if (!empty($resources)) { $insertData = []; foreach ($resources as $resourceId) { $insertData[] = [ 'role_id' => $roleId, 'resource_id' => $resourceId ]; } Db::table('role_resources')->insert($insertData); } Db::commit(); return true; } catch (\Throwable $exception) { Db::rollBack(); return false; } } /** * 获取角色资源 * * @param int $roleId 角色ID * @return array 资源ID列表 */ public function getRoleResources(int $roleId): array { if (empty($roleId)) { return []; } return Db::table('role_resources') ->where('role_id', $roleId) ->pluck('resource_id') ->toArray(); } /** * 获取用户资源 * * @param int $userId 用户ID * @return array 资源ID列表 */ public function getUserResources(int $userId): array { if (empty($userId)) { return []; } return Db::table('user_resources') ->where('user_id', $userId) ->pluck('resource_id') ->toArray(); } }