Xerakon

Experience

300

0 Best Reply Awards

  • Member Since 6 Days Ago
  • 0 Lessons Completed
  • 0 Favorites

19th September, 2018

Xerakon left a reply on Relationship Issues • 6 hours ago

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.

Xerakon left a reply on Relationship Issues • 9 hours ago

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?

Xerakon left a reply on Relationship Issues • 13 hours ago

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

Xerakon left a reply on Relationship Issues • 13 hours ago

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');
}

18th September, 2018

Xerakon left a reply on Relationship Issues • 1 day ago

@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!

Xerakon left a reply on Relationship Issues • 1 day ago

@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.

Xerakon started a new conversation Relationship Issues • 1 day ago

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()

14th September, 2018

Xerakon left a reply on Eloquent Relationship Questions • 5 days ago

I have a users table that has a many-many relationship with a practices table, connected via an id pivot table called practice_user. The practices table hasMany relationship to a doctors table, which belongsTo the practices table. What I'm trying to do, on the doctor's show blade/Controller, is display the Doctor's Name, the Practice's Name that the doctor belongs to, and the User's name that is tied to/represents the Practice that the doctor belongs to.

Xerakon left a reply on Eloquent Relationship Questions • 5 days ago

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.

Xerakon started a new conversation Eloquent Relationship Questions • 5 days ago

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!

Xerakon left a reply on Many-to-Many Relationship, Reference Table's Field • 5 days ago

I think that the tutorial I was working through had it defined as the singular. I've gone back and pluralized it!

Thanks for the help, and I'll work on digging through the documentation for the differences.

13th September, 2018

Xerakon left a reply on Many-to-Many Relationship, Reference Table's Field • 6 days ago

Yes, that is not working for some reason. I can't seem to get the DD to run, but when I use the convention $practice->user on the index page, it displays this array:

[{"id":6,"name":"Test User","email":"[email protected]","email_verified_at":null,"created_at":"2018-09-12 09:19:25","updated_at":"2018-09-12 09:19:25","pivot":{"practice_id":5,"user_id":6}}]

The $practice is in a foreach loop on the page:

@foreach ($practices as $practice)
                            <tr>
                                <th scope="row"><a href="{{route('practices.show', $practice->id)}}">{{$practice->name}}</a></th>
                                <td>{{$practice->user}}</td>
                                <td>{{$practice->city}}, {{$practice->state}}</td>
                                <td>{{$practice->created_at}}</td>
                                <td>
                                    <a class="btn btn-outline-primary" href="{{route('practices.show', $practice->id)}}">View</a>
                                    <a class="btn btn-outline-secondary" href="{{route('practices.edit', $practice->id)}}">Edit</a>
                                </td>
                            </tr>
                        @endforeach 

When I try the convention you suggested, I get "Trying to get property of non-object "

$practice->user->name gives me "Property [name] does not exist on this collection instance."

$practice->user()->name gives me"Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$name"

Xerakon started a new conversation Many-to-Many Relationship, Reference Table's Field • 6 days ago

I'm having trouble wording what I'm looking for for search purposes, so can someone help? I have two tables/models: Practice and User with a many-to-many relationship. They have a pivot table: practice_user. I've managed to get them to create/edit pretty well, but what I'm looking to do now is display the 'name' field from the User table on the PracticeModel's index page. So we can see which practice is assigned to which user.

PracticeController:

public function index()
    {
        $practices = Practice::orderBy('name', 'asc')->paginate(10);
        return view('admin.practices.index')->withPractices($practices);
    }

Practice's index.blade.php

{{$practice->user}}

When used in this manner, it displays the entire array for the correct user. However, if I try to change it to {{$practice->user->name}}, I receive a warning: "Property [name] does not exist on this collection instance."

Thanks in advance!

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.