The AttachmentController handles file uploads and deletion for patient medical records. It supports uploading documents, images, and other files associated with patient records.Controller Location:app/Http/Controllers/AttachmentController.php Middleware:auth, verified, role:admin|doctor|receptionist
POST /patients/15/attachmentsContent-Type: multipart/form-datafile: [binary file data]label: Lab Results - Blood Test January 2026
// Frontend example with FormDataconst formData = new FormData();formData.append('file', fileInput.files[0]);formData.append('label', 'Lab Results - Blood Test January 2026');await axios.post(`/patients/${patientId}/attachments`, formData, { headers: { 'Content-Type': 'multipart/form-data' }});
public function destroy(Attachment $attachment){ \Illuminate\Support\Facades\Storage::disk('public')->delete($attachment->file_path); $attachment->delete(); return redirect()->back()->with('success', 'Archivo eliminado.');}
Deleting an attachment permanently removes both the database record and the physical file from storage. This action cannot be undone.
The controller uses an action class for file upload logic:UploadAttachmentAction (app/Actions/Attachments/UploadAttachmentAction.php)This action handles:
// Delete attachment with confirmationif (confirm('Are you sure you want to delete this attachment?')) { await axios.delete(`/attachments/${attachment.id}`); // Refresh attachment list loadAttachments();}
Access Control: Only authenticated users with appropriate roles can upload attachments. Implement additional checks if needed to ensure users can only access attachments for patients they are authorized to view.
File Validation: The controller validates file size (max 10 MB) but does not restrict file types. Consider adding MIME type validation if specific file types should be blocked for security reasons.
// File too large{ "message": "The file must not be greater than 10240 kilobytes.", "errors": { "file": ["The file must not be greater than 10240 kilobytes."] }}// Missing file{ "message": "The file field is required.", "errors": { "file": ["The file field is required."] }}// Label too long{ "message": "The label must not be greater than 255 characters.", "errors": { "label": ["The label must not be greater than 255 characters."] }}