Skip to main content

Overview

The ResourceLog model provides an immutable audit trail for all resource-related actions. It tracks resource creation, updates, deletions, and other activities with full change history.

Database Table

Table Name: resource_logs The resource_logs table stores a complete audit trail of all resource operations and changes.

Properties

Fillable Attributes

netbird_id
string
The NetBird ID of the resource being logged (null for pending resources).
action
string
required
The action performed (e.g., ‘created’, ‘updated’, ‘deleted’, ‘approved’, ‘rejected’).
resource_name
string
required
The name of the resource at the time of the action.
resource_address
string
The network address of the resource at the time of the action.
performed_by
string
The identifier (typically email) of the user who performed the action.
changes
array
Array containing the specific changes made. Cast to array from JSON.

Timestamps

created_at
datetime
Timestamp when the log entry was created (when the action occurred)
updated_at
datetime
Timestamp when the log entry was last updated (typically same as created_at for immutable logs)

Casts

The model automatically casts the following attributes:
'changes' => 'array'
The changes field is stored as JSON in the database but automatically cast to an array when accessed in PHP.

Methods

logAction()

Static method to create a new log entry for a resource action.
public static function logAction(
    string $action,
    string $resourceName,
    ?string $netbirdId = null,
    ?string $resourceAddress = null,
    ?string $performedBy = null,
    ?array $changes = null
): self
Parameters:
action
string
required
The action being performed (e.g., ‘created’, ‘updated’, ‘deleted’, ‘approved’, ‘rejected’)
resourceName
string
required
The name of the resource being acted upon
netbirdId
string|null
The NetBird ID of the resource (if applicable)
resourceAddress
string|null
The network address of the resource
performedBy
string|null
Identifier of the user performing the action (typically email address)
changes
array|null
Array of changes made during this action
Returns: ResourceLog - The newly created log entry instance Example:
$log = ResourceLog::logAction(
    action: 'created',
    resourceName: 'Production Server',
    netbirdId: 'nb_123456789',
    resourceAddress: '10.0.1.50',
    performedBy: '[email protected]',
    changes: [
        'name' => 'Production Server',
        'address' => '10.0.1.50',
        'enabled' => true
    ]
);

Usage Examples

Logging Resource Creation

$resource = Resource::create([
    'netbird_id' => 'nb_123456789',
    'user_id' => $user->id,
    'created_by' => auth()->user()->email,
]);

ResourceLog::logAction(
    action: 'created',
    resourceName: 'New Server',
    netbirdId: $resource->netbird_id,
    resourceAddress: '192.168.1.100',
    performedBy: auth()->user()->email,
    changes: [
        'user_id' => $user->id,
        'created_by' => auth()->user()->email,
    ]
);

Logging Resource Updates

$resource = Resource::findOrFail($id);
$oldAttributes = $resource->getOriginal();

$resource->update(['enabled' => false]);

ResourceLog::logAction(
    action: 'updated',
    resourceName: $resource->name,
    netbirdId: $resource->netbird_id,
    resourceAddress: $resource->address,
    performedBy: auth()->user()->email,
    changes: [
        'before' => ['enabled' => true],
        'after' => ['enabled' => false],
    ]
);

Logging Pending Resource Approval

$pending = PendingResource::findOrFail($id);

ResourceLog::logAction(
    action: 'approved',
    resourceName: $pending->name,
    resourceAddress: $pending->address,
    performedBy: auth()->user()->email,
    changes: $pending->toLogChanges()
);

Logging Resource Deletion

$resource = Resource::findOrFail($id);
$resourceData = [
    'netbird_id' => $resource->netbird_id,
    'name' => $resource->name,
    'address' => $resource->address,
];

$resource->delete();

ResourceLog::logAction(
    action: 'deleted',
    resourceName: $resourceData['name'],
    netbirdId: $resourceData['netbird_id'],
    resourceAddress: $resourceData['address'],
    performedBy: auth()->user()->email,
    changes: $resourceData
);

Querying Logs

// Get all logs for a specific resource
$logs = ResourceLog::where('netbird_id', 'nb_123456789')
    ->orderBy('created_at', 'desc')
    ->get();

// Get logs by action type
$creationLogs = ResourceLog::where('action', 'created')->get();
$deletionLogs = ResourceLog::where('action', 'deleted')->get();

// Get logs for a specific user
$userLogs = ResourceLog::where('performed_by', '[email protected]')
    ->orderBy('created_at', 'desc')
    ->get();

// Get recent activity
$recentLogs = ResourceLog::orderBy('created_at', 'desc')
    ->limit(50)
    ->get();

Displaying Audit Trail

$logs = ResourceLog::where('netbird_id', $resource->netbird_id)
    ->orderBy('created_at', 'desc')
    ->get();

foreach ($logs as $log) {
    echo "{$log->created_at}: {$log->action} by {$log->performed_by}\n";
    
    if ($log->changes) {
        echo "Changes: " . json_encode($log->changes, JSON_PRETTY_PRINT);
    }
}

Common Action Types

// Creation
ResourceLog::logAction('created', $name, $netbirdId, ...);

// Update
ResourceLog::logAction('updated', $name, $netbirdId, ...);

// Deletion
ResourceLog::logAction('deleted', $name, $netbirdId, ...);

Model Location

Namespace: App\Models\ResourceLog File: app/Models/ResourceLog.php:9
The logAction() static method provides a convenient, consistent way to create log entries. Always use this method instead of directly creating ResourceLog instances.
Logs should be treated as immutable. Avoid updating or deleting log entries to maintain audit trail integrity.

Build docs developers (and LLMs) love