where('email', trim($email))->first() !== null; } public function emailExistsExcept(string $email, ?int $excludeUserId = null): bool { $builder = $this->where('email', trim($email)); if ($excludeUserId !== null && $excludeUserId > 0) { $builder->where('id !=', $excludeUserId); } return $builder->first() !== null; } protected function assignFormattedUserId(array $data): array { $insertId = (int) ($data['id'] ?? 0); $role = strtolower((string) ($data['data']['role'] ?? '')); if ($insertId < 1) { return $data; } if ($role === '') { $user = $this->find($insertId); $role = strtolower((string) ($user['role'] ?? '')); } $this->builder() ->where('id', $insertId) ->where('(formatted_user_id IS NULL OR formatted_user_id = "")') ->update([ 'formatted_user_id' => $this->formatUserId($insertId, $role), ]); return $data; } public function formatUserId(int $userId, ?string $role = null): string { $prefix = match (strtolower((string) $role)) { 'patient' => 'PAT', 'doctor' => 'PHY', }; return $prefix . str_pad((string) $userId, 7, '0', STR_PAD_LEFT); } }