Home > Back-end >  How to create a relationship between two eloquent models
How to create a relationship between two eloquent models

Time:10-11

I have two Models: Users and Appointments

class Appointments extends Model
{
    use HasFactory;

    protected $fillable = [
        'business_id',
        'user_id',
        'subject',
        'description',
        'status',
        'phone_number',
        'start_appointment',
        'end_appointment',
    ];

class User extends Authenticatable
{
protected $fillable = [
        'name',
        'email',
        'password',
        'card_id',
        'assigned_to',
        'user_type',
        'password_changed_at',
    ];
}

My relationship in Appointment Model:

public function user()
    {
        return $this->belongsToMany(User::class); 
    }

user_id and business_id correspond to user_id on the user model. I want to be able to do something like this:

 App\Models\Appointments {#5029
     id: 1,
     business_id: 24,
     status: "in_progress",
     subject: "Minima similique aspernatur temporibus corporis libero aut voluptatibus.",
     description: "The players all played at once set to work, and very soon had to kneel down on the Duchess's cook.",
     phone_number: "205-565-2817",
     user_id: 27,
     start_appointment: "2022-10-22 18:38:17",
     end_appointment: "2022-10-22 19:08:17",
     created_at: "2022-10-10 16:39:53",
     updated_at: "2022-10-10 16:39:53",
     user: App\Models\User {#5050
       id: 27,
       name: "Susan Bosco",
       email: "[email protected]",
       card_id: "6163819953",
       email_verified_at: "2022-10-10 11:41:57",
       #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
       #two_factor_secret: null,
       #two_factor_recovery_codes: null,
       #remember_token: "zTnSUsbrEL",
       current_team_id: null,
       profile_photo_path: null,
       created_at: "2022-10-10 11:41:57",
       updated_at: "2022-10-10 11:41:57",
       assigned_to: null,
       doctor_type: null,
       user_type: 0,
       password_changed_at: "2022-10-10 11:41:57",
       deleted_at: null,
        profile_photo_url: "https://ui-avatars.com/api/?name=S B&color=7F9CF5&background=EBF4FF",
     },
App\Models\User {#5059
         id: 24,
         name: "Dr. Floyd O'Keefe MD",
         email: "[email protected]",
         card_id: "1892324633",
         email_verified_at: "2022-10-10 11:41:57",
         #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
         #two_factor_secret: null,
         #two_factor_recovery_codes: null,
         #remember_token: "Dxb9oivJ7H",
         current_team_id: null,
         profile_photo_path: null,
         created_at: "2022-10-10 11:41:57",
         updated_at: "2022-10-10 11:48:55",
         assigned_to: 28,
         doctor_type: null,
         user_type: 2,
         password_changed_at: "2022-10-10 11:41:57",
         deleted_at: null,
       },
$appointments->client_name;
$appointments->business_name

This i how i get the Appointments

  public function getAppointmetns(){
        $doctors = User::where([['user_type',2],['assigned_to',auth()->id()]])->pluck('id');
        return Appointments::with(['user'])->whereIn('business_id',$doctors)->get();
        
    }

but i get the same name twice

CodePudding user response:

Assuming user_id and business_id are both representing the id of the user.

In that case :

# App/Models/Appointment.php

class Appointment 
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function business()
    {
        return $this->belongsTo(User::class, 'business_id');
    }    
}

Documentation : BelongsTo Relationship

CodePudding user response:

Just define relationship in model like:

In Appointment model write:

public function user ()
{
    return $this->your-relationship(User::class);
}

For User:

public function appointment ()
{
    return $this->your-relationship(Appointment::class);
}

CodePudding user response:

for example if you want to fetch the doctor you must fetch with user__type filtering.

//something like this
$x = $appointments->user()->where('user_type', 'doctor')->first();
$x->name;
  • Related