Răsfoiți Sursa

Patient single devices - optimize queries

Vijayakrishnan 4 ani în urmă
părinte
comite
9208783ad0
2 a modificat fișierele cu 17 adăugiri și 8 ștergeri
  1. 15 8
      app/Http/Controllers/PatientController.php
  2. 2 0
      storage/debugbar/.gitignore

+ 15 - 8
app/Http/Controllers/PatientController.php

@@ -186,14 +186,21 @@ class PatientController extends Controller
 
     public function devices(Request $request, Client $patient )
     {
-        $devices = BDTDevice::where('is_active', true)->get();
-        $devices = $devices->filter(function ($record) {
-            $matching = ClientBDTDevice
-                ::where('device_id', $record->id)
-                ->where('is_active', true)
-                ->get();
-            return count($matching) === 0;
-        });
+        // get assigned devices
+        $assignedDeviceIDs = DB::select(DB::raw("SELECT device_id from client_bdt_device where is_active = true"));
+        $assignedDeviceIDs = array_map(function($_x) {
+            return $_x->device_id;
+        }, $assignedDeviceIDs);
+
+        // get all except assigned ones
+        $devices = BDTDevice::where('is_active', true)
+            ->whereNotIn('id', $assignedDeviceIDs)
+            ->orderBy('imei', 'asc')
+            ->get();
+
+        $assignedDeviceIDs = null;
+        unset($assignedDeviceIDs);
+
         return view('app.patient.devices', compact('patient', 'devices'));
     }
 

+ 2 - 0
storage/debugbar/.gitignore

@@ -0,0 +1,2 @@
+*
+!.gitignore