Skip to main content

Overview

The Prescriptions module manages medication documentation in Nguhöe EHR. Prescriptions are created from consultations and can be previewed or downloaded as professional PDF documents using the dompdf library.
All authenticated users can access prescriptions based on their role. The system enforces strict access control to ensure privacy.

Prescription Data Model

Core Fields

FieldTypeDescription
consultation_idforeign keyLink to the consultation
patient_idforeign keyReference to patient
doctor_idforeign keyReference to prescribing doctor
itemsJSON arrayMedication items with dosage
general_instructionstextGeneral instructions for all medications

Medication Items Structure

The items field is a JSON array containing medication details:
[
  {
    "medication": "Ibuprofeno 400mg",
    "dosage": "1 tableta",
    "frequency": "Cada 8 horas",
    "duration": "5 días"
  },
  {
    "medication": "Amoxicilina 500mg",
    "dosage": "1 cápsula",
    "frequency": "Cada 12 horas",
    "duration": "7 días"
  }
]
The items array is automatically cast to/from JSON by Laravel’s Eloquent (Prescription.php:22-24).

Creating Prescriptions

Prescriptions are created automatically when a consultation includes prescription items:
// During consultation creation (ConsultationController.php:59-64)
'prescription_items' => 'nullable|array',
'prescription_items.*.medication' => 'required_with:prescription_items|string',
'prescription_items.*.dosage' => 'required_with:prescription_items|string',
'prescription_items.*.frequency' => 'nullable|string',
'prescription_items.*.duration' => 'nullable|string',
'prescription_instructions' => 'nullable|string',
The CreateConsultationAction handles prescription creation:
  1. Consultation is created
  2. If prescription items exist, create prescription
  3. Link prescription to consultation, patient, and doctor

Prescription Item Fields

Name and strength of the medicationExamples:
  • “Metformina 850mg”
  • “Losartan 50mg”
  • “Omeprazol 20mg”
Amount to take per doseExamples:
  • “1 tableta”
  • “2 cápsulas”
  • “10 ml”
How often to take the medicationExamples:
  • “Cada 8 horas”
  • “Dos veces al día”
  • “Una vez al día en ayunas”
How long to continue treatmentExamples:
  • “7 días”
  • “1 mes”
  • “Uso continuo”

PDF Generation with dompdf

Prescriptions are rendered as professional PDF documents using the dompdf library.

Preview Prescription

Display the prescription PDF in the browser: Route: prescriptions.previewPrescriptionController@show
Endpoint: GET /prescriptions/{prescription}/preview
// Preview in browser (PrescriptionController.php:62-70)
public function show(Prescription $prescription, GeneratePrescriptionPdfAction $action)
{
    $prescription->load('consultation');
    $this->authorizeAccess($prescription);

    $pdf = $action->execute($prescription);
    
    return $pdf->stream();
}

Download Prescription

Download the prescription as a PDF file: Route: prescriptions.downloadPrescriptionController@download
Endpoint: GET /prescriptions/{prescription}/download
// Download PDF (PrescriptionController.php:47-57)
public function download(Prescription $prescription, GeneratePrescriptionPdfAction $action)
{
    $prescription->load('consultation');
    $this->authorizeAccess($prescription);

    $pdf = $action->execute($prescription);
    
    $filename = "receta-{$prescription->patient->full_name}-" . now()->format('Ymd') . ".pdf";
    
    return $pdf->download($filename);
}
Filenames are automatically generated with patient name and date: receta-Juan-Perez-20260304.pdf

GeneratePrescriptionPdfAction

The GeneratePrescriptionPdfAction class handles PDF generation:
  • Loads prescription with patient and doctor data
  • Renders a Blade template with prescription details
  • Converts HTML to PDF using dompdf
  • Returns the PDF object for streaming or downloading

Access Control

Strict access control ensures prescription privacy (PrescriptionController.php:14-42):
private function authorizeAccess(Prescription $prescription): void
{
    $user = request()->user();

    // Admin can view all
    if ($user->hasRole('admin')) {
        return;
    }

    // Doctor: only their own prescriptions
    if ($user->hasRole('doctor') && $prescription->consultation->doctor_id === $user->id) {
        return;
    }

    // Patient: only their own prescriptions
    if ($user->hasRole('patient')) {
        $patient = Patient::where('user_id', $user->id)->first();
        if ($patient && $prescription->patient_id === $patient->id) {
            return;
        }
    }

    // Receptionist can view prescriptions (for delivery)
    if ($user->hasRole('receptionist')) {
        return;
    }

    abort(403, 'No tienes acceso a esta receta.');
}
  • View and download all prescriptions
  • Full access to preview and PDF generation
  • View and download only prescriptions they created
  • Access through consultation link
  • View and download only their own prescriptions
  • Access through patient portal
  • View and download all prescriptions
  • Needed for printing and delivery to patients

Patient Portal Access

Patients can view their prescriptions through the dedicated portal: Route: patient.prescriptionsPatientPortalController@prescriptions
Endpoint: GET /my-prescriptions
// Patient prescription list (PatientPortalController.php:168-186)
$prescriptions = Prescription::with(['consultation.doctor'])
    ->where('patient_id', $patient->id)
    ->latest()
    ->paginate(10);
Patients can:
  • View list of all their prescriptions
  • Preview prescriptions in browser
  • Download prescription PDFs
  • See which doctor prescribed each medication

Relationships

// Prescription.php relationships

// Belongs to Consultation
public function consultation(): BelongsTo
{
    return $this->belongsTo(Consultation::class);
}

// Belongs to Patient
public function patient(): BelongsTo
{
    return $this->belongsTo(Patient::class);
}

// Belongs to Doctor
public function doctor(): BelongsTo
{
    return $this->belongsTo(User::class, 'doctor_id');
}

Database Schema

CREATE TABLE prescriptions (
    id BIGINT PRIMARY KEY,
    consultation_id BIGINT NOT NULL REFERENCES consultations(id) ON DELETE CASCADE,
    patient_id BIGINT NOT NULL REFERENCES patients(id) ON DELETE CASCADE,
    doctor_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    items JSON NOT NULL,
    general_instructions TEXT,
    created_at TIMESTAMP,
    updated_at TIMESTAMP,
    deleted_at TIMESTAMP
);

UI Workflow

1

Create During Consultation

Add prescription items while documenting consultation
2

Auto-Generate

Prescription is automatically created and linked
3

Access from Patient Record

View prescription from patient profile or consultation details
4

Preview or Download

Click preview to view in browser or download as PDF
5

Patient Access

Patient can view and download from their portal

Example Usage

Creating a Multi-Item Prescription

// During consultation creation
[
    'prescription_items' => [
        [
            'medication' => 'Enalapril 10mg',
            'dosage' => '1 tableta',
            'frequency' => 'Una vez al día en la mañana',
            'duration' => 'Uso continuo',
        ],
        [
            'medication' => 'Metformina 850mg',
            'dosage' => '1 tableta',
            'frequency' => 'Dos veces al día con comidas',
            'duration' => 'Uso continuo',
        ],
        [
            'medication' => 'Aspirina 100mg',
            'dosage' => '1 tableta',
            'frequency' => 'Una vez al día',
            'duration' => 'Uso continuo',
        ],
    ],
    'prescription_instructions' => 'Control de presión arterial semanal. Seguimiento en 1 mes.',
]

Accessing Prescription Data

// Load prescription with relationships
$prescription = Prescription::with(['patient', 'doctor', 'consultation'])->find($id);

// Access medication items
foreach ($prescription->items as $item) {
    echo $item['medication']; // "Ibuprofeno 400mg"
    echo $item['dosage'];     // "1 tableta"
    echo $item['frequency'];  // "Cada 8 horas"
    echo $item['duration'];   // "5 días"
}

// General instructions
echo $prescription->general_instructions;

Best Practices

Complete Information

Always include medication, dosage, and frequency for patient safety

Clear Instructions

Use plain language that patients can understand

Duration Specified

Always specify how long to take the medication

General Instructions

Add important warnings or follow-up instructions
Prescriptions use soft deletes to maintain medical history. Never permanently delete prescriptions for audit and legal compliance.

Build docs developers (and LLMs) love