feat: add ActivityLog model and migration with record() helper

This commit is contained in:
GuavaPopper
2026-06-03 20:55:30 +07:00
parent 1fcad7d9b2
commit 7fb649f690
2 changed files with 58 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ActivityLog extends Model
{
protected $fillable = [
'user_id', 'user_name', 'action', 'description',
'subject_type', 'subject_id', 'ip_address',
];
public static function record(string $action, string $description, ?string $subjectType = null, $subjectId = null): void
{
static::create([
'user_id' => 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);
}
}
@@ -0,0 +1,27 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('activity_logs', function (Blueprint $table) {
$table->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');
}
};