37 lines
885 B
PHP
37 lines
885 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ActivityLog;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class ActivityLogService
|
|
{
|
|
public function log(
|
|
?User $user,
|
|
Model|string $model,
|
|
int|string|null $modelId,
|
|
string $action,
|
|
string $description,
|
|
?string $ipAddress = null
|
|
): ActivityLog {
|
|
if ($model instanceof Model) {
|
|
$modelClass = $model::class;
|
|
$modelId = $model->getKey();
|
|
} else {
|
|
$modelClass = $model;
|
|
}
|
|
|
|
return ActivityLog::query()->create([
|
|
'user_id' => $user?->id,
|
|
'model_type' => $modelClass,
|
|
'model_id' => $modelId ?? 0,
|
|
'action' => $action,
|
|
'description' => $description,
|
|
'ip_address' => $ipAddress,
|
|
'created_at' => now(),
|
|
]);
|
|
}
|
|
}
|