Skip to main content

Overview

The Patient model represents individual patients in the EHR system. It stores personal information, contact details, and comprehensive medical history including antecedents, allergies, chronic diseases, and current medications. Model Location: app/Models/Patient.php Features:
  • Soft deletes enabled
  • Polymorphic attachments support
  • Relationships with consultations, prescriptions, and payments
  • Date casting for birth dates

Database Schema

id
bigint
required
Primary key, auto-incrementing
full_name
string
required
Patient’s full name
document_id
string
Unique document identifier (DNI/CURP/etc). Nullable and unique.
birth_date
date
Patient’s date of birth. Automatically cast to Carbon date instance.
gender
enum
Patient’s gender. Possible values: male, female, other
phone
string
Contact phone number
email
string
Contact email address
address
text
Physical address
medical_antecedents
text
Previous medical history and conditions
allergies
text
Known allergies
chronic_diseases
text
Ongoing chronic conditions
current_medication
text
Currently prescribed or taken medications
notes
text
Additional notes about the patient
created_at
timestamp
Record creation timestamp
updated_at
timestamp
Record last update timestamp
deleted_at
timestamp
Soft delete timestamp (null if not deleted)

Fillable Attributes

The following attributes can be mass-assigned:
user_id
integer
Associated user ID (if patient has a user account)
full_name
string
required
Patient’s full name
document_id
string
Unique document identifier (DNI/CURP/etc)
birth_date
date
Date of birth (format: Y-m-d)
gender
string
Gender: male, female, or other
phone
string
Contact phone number
email
string
Contact email address
address
string
Physical address
medical_antecedents
text
Previous medical history
allergies
text
Known allergies
chronic_diseases
text
Chronic conditions
current_medication
text
Current medications
notes
text
Additional notes

Relationships

Attachments (Polymorphic)

Get all attachments associated with this patient.
$patient = Patient::find(1);
$attachments = $patient->attachments;

// Get specific attachment types
$medicalImages = $patient->attachments()
    ->where('label', 'medical_image')
    ->get();
Relationship Type: morphMany Related Model: App\Models\Attachment

Consultations

Get all consultations for this patient.
$patient = Patient::find(1);
$consultations = $patient->consultations;

// Get recent consultations
$recentConsultations = $patient->consultations()
    ->latest()
    ->limit(10)
    ->get();

// Eager load with doctor information
$patient = Patient::with('consultations.doctor')->find(1);
Relationship Type: hasMany Related Model: App\Models\Consultation

Prescriptions

Get all prescriptions for this patient.
$patient = Patient::find(1);
$prescriptions = $patient->prescriptions;

// Get prescriptions with medication details
$activePrescriptions = $patient->prescriptions()
    ->with('doctor')
    ->latest()
    ->get();
Relationship Type: hasMany Related Model: App\Models\Prescription

Payments

Get all payments made by this patient.
$patient = Patient::find(1);
$payments = $patient->payments;

// Get paid invoices only
$paidPayments = $patient->payments()
    ->where('status', 'paid')
    ->sum('amount');
Relationship Type: hasMany Related Model: App\Models\Payment

Type Casting

The model automatically casts the following attributes:
  • birth_date: Cast to Carbon date instance

Soft Deletes

The Patient model uses soft deletes. Deleted records are not permanently removed from the database but marked with a deleted_at timestamp.
// Soft delete a patient
$patient->delete();

// Include soft deleted records
$allPatients = Patient::withTrashed()->get();

// Get only soft deleted records
$deletedPatients = Patient::onlyTrashed()->get();

// Restore a soft deleted patient
$patient->restore();

// Permanently delete
$patient->forceDelete();

Usage Examples

Creating a Patient

use App\Models\Patient;

$patient = Patient::create([
    'full_name' => 'Juan García López',
    'document_id' => 'CURP123456789',
    'birth_date' => '1985-03-15',
    'gender' => 'male',
    'phone' => '+52 123 456 7890',
    'email' => '[email protected]',
    'address' => 'Calle Principal 123, Ciudad',
    'allergies' => 'Penicilina',
    'chronic_diseases' => 'Diabetes tipo 2',
    'current_medication' => 'Metformina 850mg',
]);

Updating a Patient

$patient = Patient::find(1);
$patient->update([
    'phone' => '+52 987 654 3210',
    'notes' => 'Updated contact information',
]);

Querying with Relationships

// Get patient with all related data
$patient = Patient::with([
    'consultations.doctor',
    'prescriptions',
    'payments',
    'attachments'
])->find(1);

// Get patients with upcoming consultations
$patientsWithConsultations = Patient::whereHas('consultations', function ($query) {
    $query->where('created_at', '>=', now()->subDays(30));
})->get();

Searching Patients

// Search by name or document ID
$results = Patient::where('full_name', 'like', '%García%')
    ->orWhere('document_id', 'like', '%123%')
    ->get();

// Get patients by age range
$seniorPatients = Patient::whereBetween('birth_date', [
    now()->subYears(80),
    now()->subYears(65)
])->get();

Working with Attachments

// Add an attachment to a patient
$patient->attachments()->create([
    'file_path' => 'patients/documents/test-results.pdf',
    'file_name' => 'test-results.pdf',
    'mime_type' => 'application/pdf',
    'file_size' => 245678,
    'label' => 'lab_results',
]);

Build docs developers (and LLMs) love