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
The NetBird ID of the resource being logged (null for pending resources).
The action performed (e.g., ‘created’, ‘updated’, ‘deleted’, ‘approved’, ‘rejected’).
The name of the resource at the time of the action.
The network address of the resource at the time of the action.
The identifier (typically email) of the user who performed the action.
Array containing the specific changes made. Cast to array from JSON.
Timestamps
Timestamp when the log entry was created (when the action occurred)
Timestamp when the log entry was last updated (typically same as created_at for immutable logs)
Casts
The model automatically casts the following attributes:
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:
The action being performed (e.g., ‘created’, ‘updated’, ‘deleted’, ‘approved’, ‘rejected’)
The name of the resource being acted upon
The NetBird ID of the resource (if applicable)
The network address of the resource
Identifier of the user performing the action (typically email address)
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
Resource Lifecycle
Request Workflow
State Changes
// 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.