Viewing file: AuditLog.php (2.09 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AuditLog extends Model { protected $fillable = [ 'entity_type', 'campaign_id', 'hilltop_account_id', 'user_id', 'action', 'details', 'status', ];
protected $casts = [ 'details' => 'array', ];
public static function log($action, $entityType, $entityId, $details = [], $status = 'success') { $log = new self(); $log->user_id = auth()->id(); $log->action = $action; $log->entity_type = $entityType; $log->details = $details; $log->status = $status;
// Handle specific entity IDs based on type if ($entityType === 'campaign') { $log->campaign_id = $entityId; } elseif ($entityType === 'user_mapping') { // For user mapping, we might need to handle hilltop_account_id differently depending on context // But for now, let's assume entityId is the primary ID if applicable, // or passed in details if it's a complex mapping }
// If details contains specific IDs, map them if (isset($details['hilltop_account_id'])) { $log->hilltop_account_id = $details['hilltop_account_id']; } if (isset($details['campaign_id'])) { $log->campaign_id = $details['campaign_id']; }
$log->save();
return $log; }
public function campaign() { return $this->belongsTo(Campaign::class); }
public function user() { return $this->belongsTo(User::class); }
public function hilltopAccount() { return $this->belongsTo(HilltopAccount::class); }
/** * Scope a query to only include user mapping audits. */ public function scopeUserMappingAudits($query) { return $query->where('entity_type', 'user_mapping'); }
/** * Scope a query to only include campaign audits. */ public function scopeCampaignAudits($query) { return $query->where('entity_type', 'campaign'); } }
|