From 5c4693577a15fad56db06aba40ea2b8e101def18 Mon Sep 17 00:00:00 2001 From: sky121113 Date: Mon, 19 Jan 2026 16:13:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(activity):=20=E4=BF=AE=E6=AD=A3=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E7=B4=80=E9=8C=84=E5=88=97=E8=A1=A8=E6=8F=8F=E8=BF=B0?= =?UTF-8?q?=E4=B8=AD=E6=9C=AA=E9=A1=AF=E7=A4=BA=E4=BD=BF=E7=94=A8=E8=80=85?= =?UTF-8?q?=E5=90=8D=E7=A8=B1=E7=9A=84=E5=95=8F=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 User 模型中加入 tapActivity 自動記錄 snapshot (name, username) - 在 UserController 手動紀錄的邏輯中補上 snapshot --- app/Http/Controllers/Admin/UserController.php | 9 +++++++++ app/Models/User.php | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 7f55913..be159cd 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -187,6 +187,15 @@ class UserController extends Controller ->causedBy(auth()->user()) ->event('updated') ->withProperties($properties) + ->tap(function (\Spatie\Activitylog\Contracts\Activity $activity) use ($user) { + // Manually add snapshot since we aren't using the model's LogOptions due to saveQuietly + $activity->properties = $activity->properties->merge([ + 'snapshot' => [ + 'name' => $user->name, + 'username' => $user->username, + ] + ]); + }) ->log('updated'); } diff --git a/app/Models/User.php b/app/Models/User.php index 6b85850..61092bc 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -57,4 +57,14 @@ class User extends Authenticatable ->logOnlyDirty() ->dontSubmitEmptyLogs(); } + + public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName) + { + $activity->properties = $activity->properties->merge([ + 'snapshot' => [ + 'name' => $this->name, + 'username' => $this->username, + ] + ]); + } }