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
Primary key, auto-incrementing
Unique document identifier (DNI/CURP/etc). Nullable and unique.
Patient’s date of birth. Automatically cast to Carbon date instance.
Patient’s gender. Possible values: male, female, other
Previous medical history and conditions
Ongoing chronic conditions
Currently prescribed or taken medications
Additional notes about the patient
Record creation timestamp
Record last update timestamp
Soft delete timestamp (null if not deleted)
Fillable Attributes
The following attributes can be mass-assigned:
Associated user ID (if patient has a user account)
Unique document identifier (DNI/CURP/etc)
Date of birth (format: Y-m-d)
Gender: male, female, or other
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',
]);