Skip to main content

Overview

The User model represents an authenticated user in the NetBird Selfservice application. It extends Laravel’s Authenticatable class and provides user management, authentication, and profile functionality.

Database Table

Table Name: users The users table stores user account information, authentication credentials, and profile data.

Properties

Fillable Attributes

name
string
required
The full name of the user.
email
string
required
The user’s email address (used for authentication and identification).
password
string
The user’s hashed password. Automatically hashed when set. Hidden from serialization.
google_id
string
The user’s Google OAuth ID for Google authentication.
avatar
string
URL or path to the user’s avatar image.

Hidden Attributes

The following attributes are automatically hidden from JSON serialization:
password
string
The hashed password (never exposed in API responses)
remember_token
string
Laravel’s remember token for persistent authentication

Timestamps

email_verified_at
datetime
Timestamp when the user’s email was verified. Cast to datetime.
created_at
datetime
Timestamp when the user account was created
updated_at
datetime
Timestamp when the user account was last updated

Casts

The model automatically casts the following attributes:
'email_verified_at' => 'datetime',
'password' => 'hashed'
The password cast automatically hashes passwords using bcrypt when set, ensuring secure password storage.

Traits

The User model uses the following Laravel traits:
HasFactory
trait
Enables model factory support for testing and seeding
Notifiable
trait
Enables notification sending capabilities (email, SMS, etc.)

Methods

initials()

Generates user initials from the user’s name.
public function initials(): string
Returns: string - The user’s initials (up to 2 characters) Example:
$user = User::find(1);
$user->name = 'John Doe';
echo $user->initials(); // Output: "JD"

$user->name = 'Jane Mary Smith';
echo $user->initials(); // Output: "JM" (takes first 2 words)

$user->name = 'Alice';
echo $user->initials(); // Output: "A"
Implementation Details:
  • Splits the name by spaces
  • Takes the first 2 words
  • Extracts the first character of each word
  • Returns the uppercase initials

Usage Examples

Creating a User

// Standard user creation
$user = User::create([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'password' => 'secret123', // Automatically hashed
]);

// User with Google OAuth
$user = User::create([
    'name' => 'Jane Smith',
    'email' => 'jane@gmail.com',
    'google_id' => 'google_oauth_id_123',
    'avatar' => 'https://lh3.googleusercontent.com/...',
]);

Using the Factory

// Create a single user for testing
$user = User::factory()->create();

// Create multiple users
$users = User::factory()->count(10)->create();

// Create user with specific attributes
$admin = User::factory()->create([
    'email' => 'admin@example.com',
    'name' => 'Admin User',
]);

Authentication

// Authenticate a user
if (Auth::attempt(['email' => $email, 'password' => $password])) {
    $user = Auth::user();
    // User authenticated successfully
}

// Get authenticated user
$user = auth()->user();
$userId = auth()->id();

Working with Initials

$user = auth()->user();

// Display user initials in avatar
echo "<div class='avatar'>{$user->initials()}</div>";

// Use in blade templates
@if($user->avatar)
    <img src="{{ $user->avatar }}" alt="{{ $user->name }}">
@else
    <div class="avatar-initials">{{ $user->initials() }}</div>
@endif

Sending Notifications

use App\Notifications\ResourceApproved;

$user = User::find(1);

// Send notification using the Notifiable trait
$user->notify(new ResourceApproved($resource));

// Send via specific channel
$user->notifyNow(new ResourceApproved($resource));

Querying Users

// Find by email
$user = User::where('email', 'john@example.com')->first();

// Find users with Google OAuth
$googleUsers = User::whereNotNull('google_id')->get();

// Find verified users
$verified = User::whereNotNull('email_verified_at')->get();

// Search by name
$users = User::where('name', 'like', '%John%')->get();

Updating User Profile

$user = auth()->user();

// Update name and avatar
$user->update([
    'name' => 'John Smith',
    'avatar' => $avatarUrl,
]);

// Update password (automatically hashed)
$user->update([
    'password' => 'newPassword123',
]);

Working with Relationships

// Get user's resources (requires Resource relationship)
// Note: Add this relationship to the User model if needed
// public function resources() {
//     return $this->hasMany(Resource::class);
// }

$user = User::with('resources')->find(1);
$resourceCount = $user->resources->count();

// Get user's pending requests
$pending = PendingResource::where('user_id', $user->id)
    ->where('status', 'pending')
    ->get();

Security Considerations

Password Hashing: Passwords are automatically hashed using the hashed cast. Never store plain-text passwords.
Hidden Attributes: The password and remember_token fields are automatically excluded from JSON responses and array conversions.
When creating users via OAuth (Google), you may not need to set a password. Ensure your authentication logic handles both password-based and OAuth users.

Model Location

Namespace: App\Models\User File: app/Models/User.php:11 Factory: Database\Factories\UserFactory
  • Resource - Users can own multiple resources
  • PendingResource - Users can create resource requests
// Create user
$user = User::create([
    'name' => 'Alice Johnson',
    'email' => 'alice@example.com',
    'password' => 'secure_password',
]);

// Display initials
echo $user->initials(); // "AJ"

// Create a resource request
$pending = PendingResource::create([
    'name' => 'Database Server',
    'address' => '10.0.1.100',
    'user_id' => $user->id,
    'requested_by' => $user->name,
    'requested_email' => $user->email,
    'status' => 'pending',
]);

// Send notification
$user->notify(new ResourceRequestSubmitted($pending));

Build docs developers (and LLMs) love