31 lines
1.1 KiB
PHP
31 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Traits;
|
||
|
|
|
||
|
|
use App\Models\ActivityLog;
|
||
|
|
use Illuminate\Support\Facades\Auth;
|
||
|
|
|
||
|
|
trait LogsActivity
|
||
|
|
{
|
||
|
|
public function logAction($action, $modelInstance, $description)
|
||
|
|
{
|
||
|
|
if (Auth::check()) {
|
||
|
|
// Pastikan model tidak null sebelum mengambil namanya
|
||
|
|
$modelName = $modelInstance ? class_basename($modelInstance) : 'System';
|
||
|
|
$modelId = $modelInstance ? ($modelInstance->id ?? null) : null;
|
||
|
|
|
||
|
|
ActivityLog::create([
|
||
|
|
'user_id' => Auth::id(),
|
||
|
|
'action' => $action,
|
||
|
|
'model' => $modelName, // Sekarang tidak akan NULL
|
||
|
|
'model_id' => $modelId,
|
||
|
|
'description' => $description,
|
||
|
|
'ip_address' => request()->ip(),
|
||
|
|
// getChanges() hanya untuk update; saat delete kita kirim null atau data asli
|
||
|
|
'properties' => ($modelInstance && method_exists($modelInstance, 'getChanges'))
|
||
|
|
? $modelInstance->getChanges()
|
||
|
|
: null,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|