Skip to content

User Model

The core user model that handles authentication and basic user information.

Overview

The User model is the foundation of the authentication system, providing Laravel Sanctum API authentication, role management with Spatie Permissions, and user impersonation support.

Attributes

AttributeTypeDescription
idintPrimary key
namestringUser's full name
emailstringUser's email address (unique)
passwordstringHashed password
email_verified_attimestampEmail verification timestamp
remember_tokenstringRemember me token
created_attimestampCreation timestamp
updated_attimestampLast update timestamp
deleted_attimestampSoft delete timestamp

Relationships

One-to-One

  • hasOne(Member) - Associated member profile

One-to-Many

  • hasMany(Cart) - User's shopping carts
  • hasMany(Order) - User's orders

Key Features

Authentication

  • Laravel Sanctum API authentication
  • Email/password authentication
  • Remember me functionality
  • Email verification support

Role Management

  • Spatie Permission role management
  • Role and permission assignment
  • Role-based access control

User Impersonation

  • Admin user impersonation support
  • Secure impersonation switching
  • Audit trail for impersonation

Data Management

  • Soft deletes for data retention
  • Filament admin panel access control
  • Comprehensive user management

Usage Examples

Creating a User

php
$user = User::create([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'password' => 'password',
]);

Assigning Roles

php
$user->assignRole('MEMBER');
$user->givePermissionTo('view-products');

Getting Associated Data

php
// Get member profile
$member = $user->member;

// Get user's carts
$carts = $user->carts;

// Get user's orders
$orders = $user->orders;

Scopes

The User model includes several useful scopes for filtering:

php
// Get verified users
User::whereNotNull('email_verified_at')->get();

// Get users with specific role
User::role('CARRIER_ADMIN')->get();

// Get users with specific permission
User::permission('manage-products')->get();
  • Member - Extended user profile
  • Cart - Shopping cart functionality
  • Order - Order management
  • Carrier - Multi-tenant organization