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
| Attribute | Type | Description |
|---|---|---|
id | int | Primary key |
name | string | User's full name |
email | string | User's email address (unique) |
password | string | Hashed password |
email_verified_at | timestamp | Email verification timestamp |
remember_token | string | Remember me token |
created_at | timestamp | Creation timestamp |
updated_at | timestamp | Last update timestamp |
deleted_at | timestamp | Soft delete timestamp |
Relationships
One-to-One
hasOne(Member)- Associated member profile
One-to-Many
hasMany(Cart)- User's shopping cartshasMany(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();