'datetime', 'attributes' => 'array', 'acknowledged' => 'boolean', 'acknowledged_at' => 'datetime', ]; /** * Accessor for event_type to map to type column */ public function getEventTypeAttribute() { return $this->type; } /** * Get the device that triggered this event */ public function device(): BelongsTo { return $this->belongsTo(Device::class); } /** * Get the position where this event occurred */ public function position(): BelongsTo { return $this->belongsTo(Position::class); } /** * Get the geofence related to this event */ public function geofence(): BelongsTo { return $this->belongsTo(Geofence::class); } /** * Get the user who acknowledged this event */ public function acknowledgedBy(): BelongsTo { return $this->belongsTo(User::class, 'acknowledged_by'); } /** * Scope for unacknowledged events */ public function scopeUnacknowledged($query) { return $query->where('acknowledged', false); } /** * Scope for events by type */ public function scopeByType($query, $type) { return $query->where('type', $type); } /** * Get event severity level */ public function getSeverity(): string { return match ($this->type) { 'alarm' => 'high', 'geofenceEnter', 'geofenceExit' => 'medium', 'overspeed' => 'medium', 'deviceOnline', 'deviceOffline' => 'low', default => 'low' }; } /** * Get event color based on type */ public function getEventColor(): string { return match ($this->type) { 'alarm' => 'red', 'geofenceEnter' => 'green', 'geofenceExit' => 'orange', 'overspeed' => 'red', 'deviceOnline' => 'green', 'deviceOffline' => 'red', default => 'gray' }; } /** * Get human readable event type */ public function getReadableType(): string { return match ($this->type) { 'geofenceEnter' => 'Geofence Enter', 'geofenceExit' => 'Geofence Exit', 'overspeed' => 'Overspeed', 'deviceOnline' => 'Device Online', 'deviceOffline' => 'Device Offline', 'alarm' => 'Alarm', default => ucfirst($this->type) }; } }