Home > Net >  How to get the all array in relation model in Larave?
How to get the all array in relation model in Larave?

Time:12-02

How to get all the array in relations model. It supposed to be 3 records in relation service, I can fetch all the services of doctor if per one record, but if I will display all the doctor with services, the services per doctor displaying only one.

In my controller

 $doctors = User::with('reviews', 'services')->role('Doctor')->where('clinic_id', $id)->get();

User Model

 public function services()
 {
    return $this->belongsTo(Service::class, 'id', 'doctor_id');
 }

enter image description here

This is the structure of service table enter image description here

I've tried hasMany but relation service is empty

CodePudding user response:

You have to change your relation in User model to hasMany as Doctor has many services per your table structure.

public function services()
{
   return $this->hasMany(Service::class, 'doctor_id', 'id');
}

and in Service model use belongsTo

  • Related