Skip to main content

Overview

The Resource model represents a NetBird resource that has been successfully created and associated with a user. This model stores the mapping between users and their NetBird resources.

Database Table

Table Name: resources The resources table stores approved and created NetBird resources with their associations.

Properties

Fillable Attributes

netbird_id
string
required
The unique identifier for the resource in NetBird’s system.
user_id
integer
required
The ID of the user who owns this resource.
created_by
string
The identifier of the user who created this resource (typically an email address).

Timestamps

The model automatically manages the following timestamps:
created_at
datetime
Timestamp when the resource was created
updated_at
datetime
Timestamp when the resource was last updated

Relationships

user()

Defines a belongs to relationship with the User model.
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
Returns: The User instance that owns this resource Example:
$resource = Resource::find(1);
$owner = $resource->user;
echo $owner->name;

Methods

isOwnedBy()

Checks if the resource is owned by a specific user.
public function isOwnedBy(User $user): bool
Parameters:
user
User
required
The user instance to check ownership against
Returns: bool - True if the resource belongs to the specified user, false otherwise Example:
$resource = Resource::find(1);
$currentUser = auth()->user();

if ($resource->isOwnedBy($currentUser)) {
    // User owns this resource
    $resource->delete();
}

Usage Examples

Creating a Resource

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

Querying Resources

// Get all resources for a user
$userResources = Resource::where('user_id', $user->id)->get();

// Get a specific resource by NetBird ID
$resource = Resource::where('netbird_id', 'nb_123456789')->first();

// Get resources with their user relationship
$resources = Resource::with('user')->get();

Checking Ownership

$resource = Resource::findOrFail($id);

if (!$resource->isOwnedBy(auth()->user())) {
    abort(403, 'Unauthorized access to resource');
}

Model Location

Namespace: App\Models\Resource File: app/Models/Resource.php:9
This model extends Illuminate\Database\Eloquent\Model and uses standard Laravel Eloquent features.

Build docs developers (and LLMs) love