Files

32 lines
863 B
PHP

<?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);
}
}