Relationship Issues

Published 3 weeks ago by Xerakon

Hey guys,

Having issues with referencing values through what I believe is relationship issues.

What I am trying to do is set up so that I can view the amount of patients that a doctor has, if they belong to the logged-in user. Right now, I can't even access the doctors that are connected to the user. I am using LaraTrust if that helps any.

From an administrator perspective, I can view everything just fine through the Doctor model. But the code I've been using authenticates users through the User model and down into the various tables to gather data. I'm not sure of a better way to do this.

For this particular piece, I'm not interested in listing out practices at all. Just any doctor that belongs to a practice associated with the logged-in user.

Schema: USERS table has a many-many relationship with a PRACTICES table. PRACTICES can have many DOCTORS, but DOCTOR can only have one PRACTICE. DOCTORS have many-many relationship with patients.

DashboardController.php

public function index()
{
    if (Auth::user()->hasRole('administrator')) {
        $doctors = Doctor::orderBy('full_name', 'asc')->paginate(10);
        return view('dashboard')->withDoctors($doctors);
    } elseif (Auth::user()->hasRole('representative')) {
        $doctors = User::find(Auth::user()->id)->practices()->doctors->orderBy('name', 'asc')->paginate(10);
        return view('dashboard')->withDoctors($doctors);
    } else {
        return view('dashboard');
    }
}

dashboard.blade.php

@role('administrator')
    @foreach ($doctors as $doctor)
        <tr>
            <th scope="row"><a href="{{route('doctors.show', $doctor->id)}}">{{$doctor->full_name}}</a></th>
            <td>{{$doctor->patients->count()}}</td>
            <td>#</td>
            <td>{{$doctor->created_at}}</td>
        </tr>
    @endforeach
@endrole
@role('representative')
    @foreach ($doctors as $doctor)
        <tr>
            <th scope="row"><a href="{{route('doctors.show', $doctor->id)}}">{{$doctor->name}}</a></th>
            <td>#</td>
            <td>#</td>
            <td>{{$doctor->created_at}}</td>
        </tr>
    @endforeach
@endrole

This is the error I receive: Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::doctors()

Vilfago

User and patient are the same ?

I think we have to view how you set the relations in the models.

But you can try at first

$doctors = User::find(Auth::user()->id)->with('practices.doctors')->orderBy('name', 'asc')->paginate(10);
tykus
tykus
3 weeks ago (690,060 XP)

You know the result of this User::find(Auth::user()->id) is this Auth::user() or this auth()->user(), right? And you can lazy load using load in place of with...

Xerakon

@Vilfago User and Patient are separate. Here is my model relations:

User has a many/many relationship with Practice. Practice hasMany Doctor, and Doctor belongsTo Practice. Doctor has a many/many relationship with Patient.

@tykus Thanks for the tips! Why is load better than with?

Snapey
Snapey
3 weeks ago (1,036,605 XP)
public function index()
{
    $user = Auth::user();

    if ($user->hasRole('administrator')) {

        $doctors = $user->doctors()->with('patients')->orderBy('full_name', 'asc')->paginate(10);

    } elseif ($user()->hasRole('representative')) {

        $doctors = $user->practices()->doctors()->orderBy('name', 'asc')->paginate(10);

    } else {

        $doctors = [];

    }
    
    return view('dashboard')->withDoctors($doctors);
}

is it doctor->name or doctor->full_name

Cronix
Cronix
3 weeks ago (783,370 XP)

Why is load better than with?

You use load when you already have the object, but now you want to load its relationships. In this case, user is always already loaded and available via Auth::user() or auth()->user(), so there is no need to retrieve the user again since it's already loaded. So in that case, just use load(relationships) on the existing user object.

Xerakon

@Snapey Using this code, it still returns the "Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::doctors()" error. Also, thank you very much for helping to streamline the code!

Snapey
Snapey
3 weeks ago (1,036,605 XP)

ok so we need to see each model to know where the missing relationship is.

Do you have a doctors() method on User model?

Do you have a doctors() method on Practice model?

Xerakon

Here are my models:

User.php

// Many Users to Many Practices relationship
public function practices() {
    return $this->belongsToMany('App\Practice');
}

Practice.php

// Many Practices to Many Users relationship
public function users() {
    return $this->belongsToMany('App\User');
}

// One Practice has Many Doctors relationship
public function doctors() {
    return $this->hasMany('App\Doctor');
}

Doctor.php

// Many Doctors to One Practice relationship
public function practices() {
    return $this->belongsTo('App\Practice');
}

// Many Doctors to Many Patients relationship
public function patients() {
    return $this->belongsToMany('App\Patient');
}

Patient.php

// Many Patients to Many Doctors relationship
public function doctors() {
    return $this->belongsToMany('App\Doctor');
}

// One Patient has Many Prescriptions relationship
public function prescriptions() {
    return $this->hasMany('App\Prescription');
}

Prescription.php

// Many Prescriptions to One Patient relationship
public function patients() {
    return $this->belongsTo('App\Patient');
}

// Many Prescriptions to Many Medications relationship
public function medications() {
    return $this->belongsToMany('App\Medication');
}

Medication.php

// Many Medications to Many Prescriptions relationship
public function prescriptions() {
    return $this->belongsToMany('App\Prescription');
}
Vilfago
$doctors = Auth::user()->load('practices.doctors')->sortBy('name', 'asc')->paginate(10);
Xerakon

@Vilfago Using that displays a full list of users - not a filtered list of doctors belonging to the user.

Snapey
Snapey
3 weeks ago (1,036,605 XP)

doctors dont belong to users... you dont have a relationship for that.... hence the error

Xerakon

Yes sir. Doctors belong to Practices, which are many-many with Users.

Is the best practice here to create a relationship from Users to Doctors?

Vilfago

What is a user if it's not a doctor neither a patient ?

Xerakon

They are users that have a Laratrust role tag of either a staff member of our distributor group, or are a sales rep that would want to log in and look at the doctor's production.

Snapey
Snapey
3 weeks ago (1,036,605 XP)

You have to concentrate on your relationships. if Doctors don't directly belong to users then how do they relate to each other?

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