From a73883cc79db122638ee8f48a1d89bbd23f4eacf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=92=E8=8A=92=E7=A7=91=E6=8A=80?= <67047984+qingmang@users.noreply.github.com> Date: Sun, 5 Jul 2020 20:15:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=87=AA=E5=AE=9A=E4=B9=89=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=8C=83=E5=9B=B4=E6=9B=B4=E6=96=B0=E8=BF=BD=E5=8A=A0?= =?UTF-8?q?=E9=83=A8=E9=97=A8=E6=95=B0=E6=8D=AE=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/permissions/controller/Role.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/catch/permissions/controller/Role.php b/catch/permissions/controller/Role.php index 65f7ace..089d229 100644 --- a/catch/permissions/controller/Role.php +++ b/catch/permissions/controller/Role.php @@ -93,6 +93,28 @@ class Role extends CatchController if (!empty($attachIds)) { $role->attach(array_unique($attachIds)); } + + // 更新department + $hasDepartmentIds = $role->getDepartments()->column('id'); + $departmentIds = $request->param('departments'); + + // 已存在部门 IDS + $existedDepartmentIds = []; + foreach ($hasDepartmentIds as $hasDepartmentId) { + if (in_array($hasDepartmentId, $departmentIds)) { + $existedDepartmentIds[] = $hasDepartmentId; + } + } + + $attachDepartmentIds = array_diff($departmentIds, $existedDepartmentIds); + $detachDepartmentIds = array_diff($hasDepartmentIds, $existedDepartmentIds); + + if (!empty($detachDepartmentIds)) { + $role->detachDepartments($detachDepartmentIds); + } + if (!empty($attachDepartmentIds)) { + $role->attachDepartments(array_unique($attachDepartmentIds)); + } return CatchResponse::success(); }