*/ use HasFactory, Notifiable, HasRoles; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ 'name', 'email', 'password', 'traccar_user_id', 'traccar_username', 'traccar_password', 'phone', 'timezone', 'is_active', 'is_admin', 'status', 'last_login_at', 'company', 'notes', 'created_by', ]; /** * The attributes that should be hidden for serialization. * * @var list */ protected $hidden = [ 'password', 'remember_token', 'traccar_password', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', 'is_active' => 'boolean', 'is_admin' => 'boolean', 'last_login_at' => 'datetime', ]; } /** * Get devices assigned to this user */ public function devices() { return $this->hasMany(Device::class); } /** * Get device groups this user has access to */ public function deviceGroups() { return $this->belongsToMany(DeviceGroup::class, 'device_group_user', 'user_id', 'device_group_id'); } /** * Get user's driver profile */ public function driver() { return $this->hasOne(Driver::class); } /** * Get notification preferences */ public function notificationPreferences() { return $this->hasOne(NotificationPreference::class); } /** * Get notification settings (new advanced system) */ public function notificationSettings() { return $this->hasOne(NotificationSetting::class); } /** * Get user's subscription */ public function subscription() { return $this->hasOne(Subscription::class); } /** * Get user's active subscription */ public function activeSubscription() { return $this->hasOne(Subscription::class)->where('status', 'active'); } /** * Get users created by this user (admin functionality) */ public function createdUsers() { return $this->hasMany(User::class, 'created_by'); } /** * Get user who created this user */ public function creator() { return $this->belongsTo(User::class, 'created_by'); } /** * Get commands sent by this user */ public function commands() { return $this->hasMany(Command::class); } /** * Check if user is admin */ public function isAdmin(): bool { return $this->is_admin || $this->hasRole('admin'); } /** * Check if user is active */ public function isActive(): bool { return $this->status === 'active' && $this->is_active; } /** * Get user's device limit based on subscription */ public function getDeviceLimit(): int { if ($this->subscription && $this->subscription->isActive()) { return $this->subscription->device_limit; } return 1; // Default limit } /** * Get the user's initials */ public function initials(): string { return Str::of($this->name) ->explode(' ') ->take(2) ->map(fn ($word) => Str::substr($word, 0, 1)) ->implode(''); } }