From 632dee13a58ff7f059f0514d55ff85b0ac5b54f4 Mon Sep 17 00:00:00 2001 From: sky121113 Date: Mon, 19 Jan 2026 16:10:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(activity):=20=E4=BF=AE=E6=AD=A3=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=80=85=E6=9B=B4=E6=96=B0=E6=99=82=E7=94=A2=E7=94=9F?= =?UTF-8?q?=E9=9B=99=E9=87=8D=E7=B4=80=E9=8C=84=E7=9A=84=E5=95=8F=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 saveQuietly 避免原生 update 事件觸發紀錄 - 手動合併屬性變更 (Attributes) 與角色變更 (Roles) 為單一操作紀錄 --- app/Http/Controllers/Admin/UserController.php | 56 ++++++++++++++----- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 027e7f9..7f55913 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -117,8 +117,7 @@ class UserController extends Controller public function update(Request $request, string $id) { $user = User::findOrFail($id); - $oldRoles = $user->roles()->pluck('display_name')->join(', '); - + $validated = $request->validate([ 'name' => ['required', 'string', 'max:255'], 'email' => ['nullable', 'string', 'email', 'max:255', Rule::unique('users')->ignore($user->id)], @@ -130,6 +129,7 @@ class UserController extends Controller 'password.confirmed' => '密碼確認不符', ]); + // 1. Prepare data and detect changes $userData = [ 'name' => $validated['name'], 'email' => $validated['email'], @@ -140,26 +140,56 @@ class UserController extends Controller $userData['password'] = Hash::make($validated['password']); } - $user->update($userData); + $user->fill($userData); + // Capture dirty attributes for manual logging + $dirty = $user->getDirty(); + $oldAttributes = []; + $newAttributes = []; + + foreach ($dirty as $key => $value) { + $oldAttributes[$key] = $user->getOriginal($key); + $newAttributes[$key] = $value; + } + + // Save without triggering events (prevents duplicate log) + $user->saveQuietly(); + + // 2. Handle Roles + $roleChanges = null; if (isset($validated['roles'])) { + $oldRoles = $user->roles()->pluck('display_name')->join(', '); $user->syncRoles($validated['roles']); - $newRoles = $user->roles()->pluck('display_name')->join(', '); if ($oldRoles !== $newRoles) { - activity() - ->performedOn($user) - ->causedBy(auth()->user()) - ->event('updated') - ->withProperties([ - 'attributes' => ['role_id' => $newRoles], - 'old' => ['role_id' => $oldRoles], - ]) - ->log('updated'); + $roleChanges = [ + 'old' => $oldRoles, + 'new' => $newRoles + ]; } } + // 3. Manually Log activity (Single Consolidated Log) + if (!empty($newAttributes) || $roleChanges) { + $properties = [ + 'attributes' => $newAttributes, + 'old' => $oldAttributes, + ]; + + if ($roleChanges) { + $properties['attributes']['role_id'] = $roleChanges['new']; + $properties['old']['role_id'] = $roleChanges['old']; + } + + activity() + ->performedOn($user) + ->causedBy(auth()->user()) + ->event('updated') + ->withProperties($properties) + ->log('updated'); + } + return redirect()->route('users.index')->with('success', '使用者更新成功'); }