feat: system tools and admin enhancements
This commit is contained in:
34
app/Services/AuditLogger.php
Normal file
34
app/Services/AuditLogger.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\AuditLog;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AuditLogger
|
||||
{
|
||||
public function log(
|
||||
Request $request,
|
||||
string $action,
|
||||
?Model $subject = null,
|
||||
array $metadata = [],
|
||||
?Model $actor = null
|
||||
): ?AuditLog {
|
||||
try {
|
||||
$actorUser = $actor ?? $request->user();
|
||||
|
||||
return AuditLog::create([
|
||||
'user_id' => $actorUser?->id,
|
||||
'action' => $action,
|
||||
'subject_type' => $subject ? get_class($subject) : null,
|
||||
'subject_id' => $subject?->getKey(),
|
||||
'metadata' => $metadata ?: null,
|
||||
'ip_address' => $request->ip(),
|
||||
'user_agent' => $request->userAgent(),
|
||||
]);
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user