From 7fb649f690fa952a9403a934da1526fdfeab00b1 Mon Sep 17 00:00:00 2001 From: GuavaPopper Date: Wed, 3 Jun 2026 20:55:30 +0700 Subject: [PATCH] feat: add ActivityLog model and migration with record() helper --- app/Models/ActivityLog.php | 31 +++++++++++++++++++ .../2026_06_03_create_activity_logs_table.php | 27 ++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 app/Models/ActivityLog.php create mode 100644 database/migrations/2026_06_03_create_activity_logs_table.php diff --git a/app/Models/ActivityLog.php b/app/Models/ActivityLog.php new file mode 100644 index 0000000..be4ce03 --- /dev/null +++ b/app/Models/ActivityLog.php @@ -0,0 +1,31 @@ + auth()->id(), + 'user_name' => auth()->user()?->name, + 'action' => $action, + 'description' => $description, + 'subject_type' => $subjectType, + 'subject_id' => $subjectId !== null ? (string) $subjectId : null, + 'ip_address' => request()->ip(), + ]); + } + + public function user() + { + return $this->belongsTo(User::class); + } +} diff --git a/database/migrations/2026_06_03_create_activity_logs_table.php b/database/migrations/2026_06_03_create_activity_logs_table.php new file mode 100644 index 0000000..710eae5 --- /dev/null +++ b/database/migrations/2026_06_03_create_activity_logs_table.php @@ -0,0 +1,27 @@ +id(); + $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete(); + $table->string('user_name')->nullable(); + $table->string('action'); + $table->string('description'); + $table->string('subject_type')->nullable(); + $table->string('subject_id')->nullable(); + $table->string('ip_address', 45)->nullable(); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('activity_logs'); + } +};