Skip to main content

Overview

The Usuario model extends Laravel’s Authenticatable class and represents authenticated users in the DentControl system. This includes dentists, assistants, and superadmins who manage clinic operations.

Table Schema

table
string
default:"usuario"
Database table name
primaryKey
integer
default:"id_usuario"
Primary key column (auto-incrementing)
timestamps
boolean
default:"true"
Includes created_at and updated_at columns

Fields

Clinic Association

id_clinica
integer
required
Foreign key to the clinic this user belongs to

Personal Information

nombre
string
required
First name
apellido_paterno
string
required
Paternal surname
apellido_materno
string
Maternal surname (optional)

Professional Information

cedula_profesional
string
Professional license number. Unique and nullable.

Authentication

nom_usuario
string
required
Username for login. Must be unique.
password
string
required
Hashed password. Automatically hashed by Laravel.

Authorization

rol
enum
required
User role: superadmin, dentista, or asistente

Status

estatus
enum
default:"activo"
User status: activo or baja

Hidden Fields

The following fields are automatically hidden from JSON responses:
  • password
  • remember_token

Casts

password
hashed
Automatically hashed when saved to database

Relationships

clinica

Type: belongsTo A user belongs to one clinic.
$usuario->clinica; // Clinica model instance
Foreign Key: id_clinica

citas

Type: hasMany A user (dentist) has many appointments.
$usuario->citas; // Collection of Cita models
Foreign Key: id_usuario on citas table

notasEvolucion

Type: hasMany A user can create many progress notes.
$usuario->notasEvolucion; // Collection of NotasEvolucion models
Foreign Key: id_usuario on notas_evolucion table

Example Usage

Creating a New User

use App\Models\Usuario;

$usuario = Usuario::create([
    'id_clinica' => 1,
    'nombre' => 'Juan',
    'apellido_paterno' => 'García',
    'apellido_materno' => 'López',
    'cedula_profesional' => '12345678',
    'nom_usuario' => 'jgarcia',
    'password' => 'securePassword123', // Will be auto-hashed
    'rol' => 'dentista',
    'estatus' => 'activo'
]);

Authentication

use Illuminate\Support\Facades\Auth;

// Attempt login
$credentials = [
    'nom_usuario' => 'jgarcia',
    'password' => 'securePassword123'
];

if (Auth::attempt($credentials)) {
    $usuario = Auth::user();
    // User is authenticated
}

Retrieving User with Relationships

// Get user with clinic and appointments
$usuario = Usuario::with(['clinica', 'citas'])
    ->find($id);

// Get user with upcoming appointments
$usuario = Usuario::with(['citas' => function ($query) {
    $query->where('fecha', '>=', now())
          ->orderBy('fecha', 'asc');
}])->find($id);

Filtering by Role

// Get all dentists in a clinic
$dentistas = Usuario::where('id_clinica', $clinicaId)
    ->where('rol', 'dentista')
    ->where('estatus', 'activo')
    ->get();

Authentication Methods

The model overrides default authentication methods:
// Returns the password field
public function getAuthPassword()

// Returns the custom identifier name
public function getAuthIdentifierName() // Returns 'id_usuario'

Model Definition

Location: app/Models/Usuario.php
protected $fillable = [
    'id_clinica',
    'nombre',
    'apellido_paterno',
    'apellido_materno',
    'cedula_profesional',
    'nom_usuario',
    'password',
    'rol',
    'estatus'
];

protected $hidden = [
    'password',
    'remember_token',
];

protected $casts = [
    'password' => 'hashed',
];

Build docs developers (and LLMs) love