Back

Laravel 5.3 Dependant Dropdown functionality issue


Hi All,

I'm attempting to create a successful dependant dropdown menu. I've run into some problems... I want to recall a foreign key on a previously used table in the drop-down dependancy. I'm trying to make it work. How do you go about recalling a table to use a foreign key in that table

example:

I pick a unit in a hospital - Spruce The Spruce unit houses x amount of patients. After Spruce is selected, through the magic of Jquery, ajax, json, and laravel, the patients on Spruce unit are displayed in the dropdown. I select John Doe from the PatientNames drop down, through the dependency process, ajax successfully outputs John Doe's correct room on the Spruce unit.

To this point, everything is successful... my problem begins when laravel 's testController attempts to access the $diagnosis variable to contribute to the diagnosis drop down. Since the diagnosis table in the database only consists of the diagnosis's alone. It has to rely on the 'rooms' table 'diagnosis_id' foreign key, pulling the 'id' primary key from the 'diagnosis' table. But the way it's set up to this point is to look for the foreign key 'diagnosis_id' on the 'diagnosis' table. Obviously, this doesn't exist because of the limitations.

How would I get this to work by recalling to the 'rooms' table for the 'diagnosis_id' foreign key, and how can it ONLY call the 'diagnosis_id' of that previously selected room?

TestController.php


<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\PatientNames; use App\Rooms; use App\Units; class TestController extends Controller { // TestController - API Function public function testIndex(Request $request) { $units=Units::all();//get data from table return view('testview', compact('units'));//sent data to view } public function findPatientName(Request $request) { // $request->id here is the id of our chosen option id. $inpatients = PatientNames::select('patient_name','id')->where('unit_id',$request->id)->take(100)->get(); return response()->json($inpatients); // Then sent this data to ajax success. } public function findRoom(Request $request){ // This variable controls the Rooms Select Field #3 $rooms = Rooms::select('room','id')->where('inpatient_id',$request->id)->take(100)->get(); return response()->json($rooms); // Then sent this data to ajax success. } public function findDiagnosis() { // This variable controls the Rooms Select Field #4 $diagnosis = Rooms::find(1)->diagnosis; return response()->json($diagnosis); // Then sent this data to ajax success. } } --- Rooms.php Model --- <?php namespace App; use Illuminate\Database\Eloquent\Model; /** * App\Rooms * @property int $id * @property string $room * @property int $unit_id * @property int $diagnosis_id * @property int $inpatient_id * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at * @method static whereCreatedAt($value) * @method static whereDiagnosisId($value) * @method static whereId($value) * @method static whereInpatientId($value) * @method static whereRoom($value) * @method static whereUnitId($value) * @method static whereUpdatedAt($value) */ class Rooms extends Model { // Select Field #3 Data Model Class: Rooms protected $table='rooms'; // public function diagnosis() { return $this->hasOne('App\Diagnosis'); } } --- Diagnosis.php Model --- <?php namespace App; use App\PatientNames; use Illuminate\Database\Eloquent\Model; /** * App\Diagnosis * @property int $id * @property string $diagnosis_type * @property string $abbreviation * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at * @method static whereSelect($value) * @method static whereAbbreviation($value) * @method static whereCreatedAt($value) * @method static whereDiagnosisType($value) * @method static whereId($value) * @method static whereUpdatedAt($value) */ class Diagnosis extends Model { // Select Field #5 Data Model Class: Diagnosis protected $table='diagnosis'; // public function room() { // return $this->belongsTo('App\Rooms'); } } --- testview.blade.php --- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Laravel 5.3 Dynamic Dropdown with AJAX</title> </head> <body style="width: 900px; margin: 0 auto"> <div style="width: 900px; margin: 50px auto; align-self: center"> <h2 style="width: 500px; margin: 50px auto; text-align: center">Laravel 5.3 Dynamic Dropdown w/ AJAX</h2> <span>Units: </span> <select style="width: 200px" class="units" id="unit_id"> <option value="0" disabled selected>Select A Unit</option> @foreach($units as $unit) <option value="{{$unit->id}}">{{$unit->unit}}</option> @endforeach </select> <span>Patient Name: </span> <select style="width: 200px" class="patient_name"> <option value="0" disabled selected>Find Patient</option> </select> <span>Room: </span> <select style="width: 200px" class="room"> <option value="0" disabled selected>Find Room</option> </select> <span>Diagnosis: </span> <select style="width: 200px" class="diagnosis"> <option value="0" disabled selected>Find Diagnosis</option> </select> </div> <script src="{{ asset('js/jquery-3.1.1.js') }}"></script> <script type="text/javascript"> $(document).ready(function() { $(document).on('change', '.units', function () { //console.log("hmm, it changed"); var unit_id = $(this).val(); //console.log(cat_id); var div = $(this).parent(); var op = " "; $.ajax({ type: 'get', url: '{!!URL::to('findPatientName')!!}', data: {'id': unit_id}, success: function (data) { console.log('success'); console.log(data); console.log(data.length); op += '<option value="0" selected disabled>Choose A Patient</option>'; for (var i = 0; i < data.length; i++) { op += '<option value="' + data[i].id + '">' + data[i].patient_name + '</option>'; } div.find('.patient_name').html(" "); div.find('.patient_name').append(op); }, error: function () { } }); }); $(document).on('change', '.patient_name', function () { //console.log("hmm, it changed"); var patient_id = $(this).val(); //console.log(cat_id); var div = $(this).parent(); var op = " "; $.ajax({ type: 'get', url: '{!!URL::to('findRoom')!!}', data: {'id': patient_id}, success: function (data) { console.log('success'); console.log(data); console.log(data.length); op += '<option value="0" selected disabled>Choose A Room</option>'; for (var i = 0; i < data.length; i++) { op += '<option value="' + data[i].id + '">' + data[i].room + '</option>'; } div.find('.room').html(" "); div.find('.room').append(op); }, error: function () { } }); }); $(document).on('change', '.room', function () { //console.log("hmm, it changed"); var diagnosis_id = $(this).val(); //console.log(cat_id); var div = $(this).parent(); var op = " "; $.ajax({ type: 'get', url: '{!!URL::to('findDiagnosis')!!}', data: {'id': diagnosis_id}, success: function (data) { console.log('success'); console.log(data); console.log(data.length); op += '<option value="0" selected disabled>Choose Diagnosis</option>'; for (var i = 0; i < data.length; i++) { op += '<option value="' + data[i].id + '">' + data[i].diagnosis + '</option>'; } div.find('.diagnosis').html(" "); div.find('.diagnosis').append(op); }, error: function () { } }); }); }); </script> </body> </html> ---

Sign in to participate in this thread!


We'd like to thank these amazing companies for supporting us