fix(activity): 修正操作紀錄列表描述中未顯示使用者名稱的問題

- 在 User 模型中加入 tapActivity 自動記錄 snapshot (name, username)

- 在 UserController 手動紀錄的邏輯中補上 snapshot
This commit is contained in:
2026-01-19 16:13:22 +08:00
parent 632dee13a5
commit 5c4693577a
2 changed files with 19 additions and 0 deletions

View File

@@ -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');
}

View File

@@ -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,
]
]);
}
}