feat: add ActivityLog model and migration with record() helper
This commit is contained in:
@@ -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');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user