Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent

I'm new to Laravel, and I'm loving the framework more and more everyday. I've only had one issue, and I can't figure it out. Hope some of you can. We're going to use my "Patient" model as an example. I have a route '/patient/view/{id}' that passes a value to a method in my controller. That controller finds the patient using model::find(). The problem I'm having is sometimes it doesn't grab new info. I'll go view patient ID 4, and then go to 8, and it's showing me info for patient 4 still. I'll provide some info below.

Route:

Route::get('/patient/view/{id}',['as' => 'Id','middleware' => 'auth','uses' => 'PatientController@showPatient']);

Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Patient extends Model
{

	/* Has relations to:
	/* Customers
	/* Facilities
	/* Policies
	/* Entities
	/* Studies
	/* Audit Trails

	***********************/

    public function scopeCustomer($query,$customer){
        return $query->where('px_customer',$customer);
    }

    public function scopeFacility($query,$facility){
        return $query->where('px_facility',$facility);
    }

  public function PxCustomer(){
		return $this->hasOne('App\Customer','id','px_company');
	}
    
    public function PxFacility(){
    	return $this->hasOne('App\Facility','id','px_facility');
    }

    public function Studies(){
    	return $this->hasMany('App\Study','st_px_id');
    }

    public function AuditTrail(){
    	return $this->hasMany('App\PatientAuditTrail','aud_patient');
    }

    public function getDisplayNameAttribute(){
        return $this->attributes['px_family_name'].', '.$this->attributes['px_given_name'].' '.$this->attributes['px_middle_name'];
    }

    public function getFormattedDateAttribute(){
        return date("m/d/Y",strtotime($this->attributes['px_dob']));
    }

    public function getFormattedSexAttribute(){
        switch($this->attributes['px_sex']){
            case 'F':
                return 'FEMALE';
            case 'M':
                return 'MALE';
            default:
                return 'UNK';
        }
    }

    protected $table = 'patients';

    protected $fillable = [
    	'px_customer_pxid',
        'px_facility',
        'px_family_name',
        'px_customer',
        'px_given_name',
        'px_middle_name',
        'px_dob',
        'px_sex',
        'px_address',
        'px_city',
        'px_state',
        'px_zip',
        'px_primary_phone',
        'px_secondary_phone',
        'px_emergency_contact',
        'px_emergency_contact_phone',
        'px_primary_insurance',
        'px_primary_insurance_policy',
        'px_secondary_insurance',
        'px_secondary_insurance_policy'
    ];
}

Controller:

public function showPatient($Id){

		if(in_array(Auth::user()->user_group,[3,4])){
			$Patient = Patient::facility(Auth::user()->user_facility)->find($Id)->first();
		}else{
			$Patient = Patient::customer(Auth::user()->user_customer)->find($Id)->first();
		}

		$Studies = Study::patient($Id)->get();

		if($Patient == null){
			return view('patients.notfound',['PageTitle' => 'Error: Patient Not Found','Menu' => Controller::getMenu('patients')]);
		}else{
			return view('patients.view',['PageTitle' => 'View Patient','Menu' => Controller::getMenu('patients'),'Patient' => $Patient,'Studies' => $Studies]);
		}

	}

and the relevant piece from the view page

<div class="row">
			<div class="col-md-4 col-md-offset-2">
				<div class="panel panel-info">
					<div class="panel-heading">
						<div class="panel-title">
							Patient Information
						</div>
					</div>
					<div class="panel-body">
						<table class="unity-info-table">
							<tr>
								<td>Name</td>
								<td><?php echo $Patient->display_name; ?></td>
							</tr>
							<tr>
								<td>DOB</td>
								<td><?php echo $Patient->formatted_date; ?></td>
							</tr>
							<tr>
								<td>Sex</td>
								<td><?php echo $Patient->formatted_sex; ?></td>
							</tr>
							<tr>
								<td>Address</td>
								<td><?php echo $Patient->px_address; ?><br><?php echo $Patient->px_city; ?> <?php echo $Patient->px_state; ?>, <?php echo $Patient->px_zip; ?></td>
							</tr>
							<tr>
								<td>Phone</td>
								<td><?php echo $Patient->px_phone; ?></td>
							</tr>
						</table>
					</div>
				</div>
			</div>
		</div>

the values in that table are stuck on a previously viewed patient. I should also point out that this doesn't happen every time I load a patient. But once it's stuck on one, it's stuck.

Last updated 3 years ago.
0

Bump bump

0

Probably just me, but I can't wrap my head around what you're trying to achieve with this:

Patient::customer(Auth::user()->user_customer)->find($Id)->first();

The customer(Auth::user()->user_customer) will set a filter on px_customer, but won't find($id) overwrite that and just do a select on the primary key?

Last updated 9 years ago.
0

It's my understanding that you can call ->find() after a scope, and it will "find" in the filtered resultset. Am I wrong?

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

rberlin01 rberlin01 Joined 5 Sep 2015

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.