Eloquent Relationship Questions

Posted 1 month 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.

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 sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.