Xerakon

Xerakon

Member Since 1 Month Ago

Experience Points 390
Experience
Level
Lessons Completed 0
Lessons
Completed
Best Reply Awards 0
Best Answer
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

29 Oct
2 weeks ago

Xerakon left a reply on Pass Data To Form On Another Page

@shez1983 The script is just a show page. They would click essentially on a link button that takes them to a create page for a prescription.

Xerakon left a reply on Pass Data To Form On Another Page

Updated. The correct route for show is test.com/scripts/34. And make sure to look at it. scripts and prescriptions are two different models/routes.

Xerakon started a new conversation Pass Data To Form On Another Page

I am attempting to essentially pass a variable from one page url to another to assist in filling out a form.

I have a Script model and a Prescription model. Each script can have multiple prescriptions. So on the Scripts show page, I'm wanting to have a create button that allows users to click it, and it sends them to the Prescriptions create page, but auto-fills out the script_id for the user in the controller. This way it is definitively tagged to the correct script.

So if someone clicks on 'Create Prescription' on test.com/scripts/34/show, it takes them to test.com/prescriptions/create and automatically applies 34 as the script_id field - ideally either a hidden field, or directly passed to the controller somehow so that the user can't tamper with it.

24 Sep
1 month ago

Xerakon left a reply on Relationship Issues

@Snapey I have tried this, but it appears that since the users and practices relationship is a many to many, and the practices to doctors is a many-to-1, it won't work.

From what I've read, hasManyThrough needs to be a chain of many-to-1s

20 Sep
1 month ago

Xerakon left a reply on Relationship Issues

Doctors belong to a Practice, which belongs to a User. However, there are some views where it is helpful to list off all of the Doctors attributed to a User, rather than the Practices, then the Doctors.

19 Sep
1 month ago

Xerakon left a reply on Relationship Issues

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

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

@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

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');
}
18 Sep
1 month ago

Xerakon left a reply on Relationship Issues

@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

@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

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

14 Sep
1 month ago

Xerakon left a reply on Eloquent Relationship Questions

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

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

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

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.

13 Sep
2 months ago

Xerakon left a reply on Many-to-Many Relationship, Reference Table's Field

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

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!