Eloquent Relationship Questions

Posted 9 months ago by Xerakon

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.


$doctor = Doctor::findOrFail($id);
$practice_id = $doctor->practice_id;
$practice = Practice::find($practice_id);
return view ('doctors.show')->withDoctor($doctor)->withPractice($practice);


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.


Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.