0

Model Doctor

class Doctor extends Model
{
    public function addresses() {
        return $this->belongsTo(Doctor::class);
    }
}

Model Address

 class Address extends Model
    {
      public function doctors() {
          return $this->hasMany(Address::class);
      }
    }

DoctorsController

class DoctorsController extends Controller
{
    public function index()
    {
        $doctors = Doctor::with('addresses')->get();

        return view('doctors.index',compact('doctors'));
    }
}

Blade

@foreach($doctors as $doctor)
    {{ $doctor->name }}
    @foreach($doctor->addresses as $address)
        {{ $address->city }}
    @endforeach

@endforeach

I have an error

Invalid argument supplied for foreach()

I tried to make a relation between Doctor and Address, but it doesn't work. If i try dd($doctor->addresses) i have null.

Olek Szewczak
  • 23
  • 1
  • 1
  • 5

2 Answers2

0

You reference the same class in your relations ("Doctor belongs to Doctor"), that probably cannot work.

Try:

class Doctor extends Model
{
    public function addresses() {
        return $this->hasMany(Address::class);
    }
}
 class Address extends Model
    {
      public function doctors() {
          return $this->belongsTo(Doctor::class);
      }
    }
Tobias K.
  • 2,997
  • 2
  • 12
  • 29
  • now i have "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'addresses.doctor_id' in 'where clause' (SQL: select * from addresses where addresses.doctor_id in – Olek Szewczak Jul 01 '18 at 13:55
  • Well, what is the structure of the `addresses` table? Laravel just assumes the FK is named `CLASS_id` so it "guesses" `doctor_id`. If it is something else you need to provide that using the second argument to `hasMany`, e.g.: `$this->hasMany(Address::class, 'id_of_doctor')` - see also the "One To Many" section in https://laravel.com/docs/5.6/eloquent-relationships – Tobias K. Jul 01 '18 at 13:59
-1

does it make sense that a doctor has many addresses and a address has many doctors? basing on your models you do have a many to many relationship between doctors and addresses?

Why not you can do it this way. a doctor has many addresses? so one to many relationship

then your models would be like this way.

Doctor model

class Doctor extends Model
{
    public function addresses() {
        return $this->hasMany('App\Address','DoctorId');// you need to indicate the foreign key if you didn't follow the laravel naming convention
    }
}

address model

class Address extends Model
{
    public function doctor() {
        return $this->hasOne('App\Doctor','DoctorId');// you need to indicate the foriegn key if you didn't follow the Laravel naming convention
      }
}

you controller

class DoctorsController extends Controller
{
    public function index()
    {
        $doctors = Doctor::all();//or Doctor::where('something','=','value')->get();

        return view('doctors.index',compact('doctors'));
    }
}

your view

@foreach($doctors as $doctor)
    {{ $doctor->name }}
    @foreach($doctor->addresses as $address)
        {{ $address->city }}
    @endforeach

@endforeach
Kevin Loquencio
  • 391
  • 1
  • 4
  • 16