By writing code. I think we need a bit more context. Maybe show what you have so far
Laravel usertypes
How do i fetch patients from a users table into the patients table using usertypes?
@Sinnbeck so i am working on a hospital management system. i have assigned roles to users using usertypes and seeded data into the database. in my users table now i have users with usertypes from 0 to 7 where 0 is for patients and therefore renders the patient dashboard whenever he logs in. But now i need this patient in the patients table but i don't know how to go about it. I don't know if i'm making sense but let me know if you want more clarity thanks
Round here, they fetch patients with two people and a trolley. But I don't think they put them on a table unless they are having an operation. Not sure what usertypes means. Is that doctors and nurses because I like playing that?
@Snapey I think they’re wanting to move someone from the addict surgery to the regular patient surgery. A usertype must be some form of hospital bed or trolley.
@kokoshneta hmm, I've met your usertype before
@Snapey lol. so i am working on a hospital management system. i have assigned roles to users using usertypes and seeded data into the database. in my users table now i have users with usertypes from 0 to 7 where 0 is for patients and therefore renders the patient dashboard whenever he logs in. But now i need this patient in the patients table but i don't know how to go about it. I don't know if i'm making sense but let me know if you want more clarity thanks
@saqar if this is as simple as you describe (and assuming there is a usertype or similar column on the users table):
$patients = User::where('usertype', 0)->get();
Or, if you want the avoid magic numbers like this, use an Enum, so the query code is readable:
$patients = User::where('usertype', UserType::PATIENT)->get();
@tykus i thought something like this might work too, but i have a foreign key that references the id on the patients table. But since there is no patient in the patient's table but the users, i have an integrity constraint violation error
@saqar so what is the purpose of the usertype value of 0 then???
I suppose you have a patients table with a user_id column?
Patient::selectRaw('patients.*, users.name, users.surname, /* etc */')
->join('users', 'patients.user_id', 'users.id')
->get()
@tykus this is tiring. I had the user id but removed it. The first code you sent was working until i tried to update that patient's details from the user table. An error "Attempt to read property "name" on null" surfaced from my update blade. And when adding a new patient by form it goes to the user table instead of the patients
@saqar Share all of the relevant code, including the (edit) controller action and update view template
@tykus As at now anytime i want the receptionist to add a patient it goes into the users table instead of the patient and throws an error. And when i try to update after getting the patients from the user table as i told you earlier that error occurs.
This is the (edit) controller action code
public function editPatient(Request $request , $id){
$patient=Patient::find($id);
$patient->first_name=$request->name;
$patient->email=$request->email;
$patient->address=$request->address;
$patient->birth_date=$request->birth_date;
$patient->phone=$request->phone;
$patient->gender=$request->gender;
$patient->reg_date=$request->reg_date;
$patient->save();
return redirect()->back->with('message','Patient Details updated Successfully');
}
This is the update patient view code
@include('receptionist.css')
@include('receptionist.banner') <!-- partial:partials/_sidebar.html -->
@include('receptionist.sidebar')
<!-- partial -->
@include('receptionist.navbar')
<!-- partial -->
<div class="container-fluid page-body-wrapper">
<div class="container" align="left" style="padding-top:100px;">
@if (session()->has('message'))
<div class="alert alert-sucess">
<button type="button" class="close" data-dismiss="alert">x </button>
{{session()->get('message')}}
</div>
@endif
<form action="{{url('editPatient')}}" method="POST" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" style="color:black" name="name" value="{{$patient->name}}" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="text" class="form-control" style="color:black" name="email" value="{{$patient->email}}" required>
</div>
<div class="mb-3">
<label for="address" class="form-label">Address</label>
<input class="form-control" id="address" style="color:black" name="address" value="{{$patient->address}}">
</div>
<div class="mb-3">
<label for="birth_date" class="form-label">Birth Date</label>
<input class="form-control" style="color:black" type="birth_date" name="birth_date" id="birth_date"
placeholder="Select Date" value="{{$patient->birth_date}}"
>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone</label>
<input class="form-control" style="color:black" type="number" name="phone" id="phone"
placeholder="Select Date" value="{{$patient->phone}}"
>
</div>
<div class="mb-3">
<label for="birth_date" class="form-label">Gender</label>
<select class="form-control" type="text" name="doctor" style="color:black" id="doctor">
<option value="{{$patient->gender}}">Select Gender</option>
<option>Male</option>
<option>Female</option>
</select>
</div>
<div class="mb-3">
<label for="reg_date" class="form-label">Registration Date</label>
<input class="form-control" style="color:black" type="date" name="reg_date" id="reg_date"
placeholder="Select Date" value="{{$patient->reg_date}}"
>
</div>
<button type="submit" class="btn btn-outline-dark">Update</button>
</form>
</div>
</div>
@include('receptionist.script')
in case you need more code let me know. I might just send the whole project over to you. Thanks in advance
@saqar a few comments:
- by convention, the
editcontroller action fetches the Model record and returns the (form)) view, while theupdatecontroller action performs the update. - by convention, the form method is
PUTwhenever we are updating records, but perhaps your route is defined as aPOSTverb? - there is no ID in the form action; how does the framework route the request correctly to the
editPatientaction? - Your error message says
Attempt to read property "name" on null"which means there is no Patient record for the given$id- you will instead getnull. You can mitigate for this usingPatient::findOrFail($id)instead offind($id). - You seem to be attempting to access a
nameproperty on the Patient instance, but in theupdateaction, there is only afirst_nameproperty being updated, so which is itnameorfirst_name???
I don't see the connection to User here... how are User and Patient models related?
@tykus well noted. i tried your Patient::findOrFail($id) but it didn't work. i corrected the first_name error too but to no avail.
And about the User and Patient models being related, what relationship do you think should be between them. Cos any attempt on adding a patient to the Patient table fails and rather tries to insert in the User table.
If i had a way to send you the project file so you can see for yourself it would be better
i tried your Patient::findOrFail($id) but it didn't work
What does "it didn't work" mean - clarify?
And about the User and Patient models being related, what relationship do you think should be between them
I don't know - this is not my app. Is the Patient also a User; or is is something different.
any attempt on adding a patient to the Patient table fails and rather tries to insert in the User table
How can this happen? Do you have the $table property set incorrectly on the Patient model?
It is really difficult to help without context.
@tykus What does "it didn't work" mean - clarify? The error Attempt to read property "name" on null still shows
I don't know - this is not my app. Is the Patient also a User; or is is something different. yes the Patient is also a User.
How can this happen? Do you have the $table property set incorrectly on the Patient model? All i have in my patient model is this relationship public function user() { return $this->belongsTo(User::class); }
@saqar okay... lets take a step back to the controller action that returns the update patient view, can you show that please?
@tykus this is for the update public function updatePatient($id){
$patient=Patient::findOrFail($id);
return view('receptionist.update-patient',compact("patient"));
}
this is for editPatient form public function editPatient(Request $request , $id){
$patient=Patient::findOrFail($id);
$patient->name=$request->name;
$patient->email=$request->email;
$patient->address=$request->address;
$patient->birth_date=$request->birth_date;
$patient->phone=$request->phone;
$patient->gender=$request->gender;
$patient->reg_date=$request->reg_date;
$patient->save();
return redirect()->back->with('message','Patient Details updated Successfully');
}
@saqar okay, so now that you use findOrFail, whenever you arrive at the receptionist.update-patient view, you must have a $patient variable that contains a Patient instance. Similarly, in the editPatient Controller action, you are now using the findOrFail method, so you must be working with a Patient instance if an Exception is not thrown. We should have moved beyond the Attempt to read property "name" on null error???
I am still wondering how this form action is supposed to be working; there is no reference to the patient ID in the URL - what does the corresponding Route definition(s) look like?
<form action="{{url('editPatient')}}" method="POST" enctype="multipart/form-data">
@tykus what does the corresponding Route definition(s) look like?
Route::post('/editPatient/{id}',[PatientController::class, 'editPatient']);
@saqar okay, so you can see how this is not the same as the form action because you never pass the ID, right?
<form action="/editPatient/{{$patient->id}}" method="POST">
@csrf
Checklist:
- Level 1
- No videos watched
- Very little context
- Guaranteed headaches
@lancashireman LOL. Epic Reply
Please or to participate in this conversation.