On doctors/show.blade.php, I was able to pull up the User's name with this foreach loop:
@foreach($practice->users as $user)
{{$user->name}}
@endforeach
But I feel as if I should be able to accomplish this without needing a loop.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm trying to determine the best way to set this up, as my database schema is very heavily involved with many-to-many relationships.
Currently, it looks something like this:
users <---> practices <---> doctors <---> patients <---> orders <---> medications
Every relationship is many/many, EXCEPT that practices can have many doctors where doctors can only have one practice.
I've got so far using the documentation and tutorials down to the doctors table, and now I'm trying to determine how it might be possible to display the user's name next to the doctor they represent.
For this example, there is the users, practices, practice_user, and doctors table. Doctors table has the foreign key of practice_id to reference practices.
DoctorController.php
$doctor = Doctor::findOrFail($id);
$practice_id = $doctor->practice_id;
$practice = Practice::find($practice_id);
return view ('doctors.show')->withDoctor($doctor)->withPractice($practice);
doctors/show.blade.php
Doctor: {{$doctor->name}}
Practice: {{$practice->name}}
Representative: ???
Using the controller info, I can pull up the doctor and associated practice by passing through the foreign key id into the practice table. But I can't determine how I need to pass this up to determine the user.
Thanks!
Please or to participate in this conversation.