模范医生
class Doctor extends Model
{
public function addresses() {
return $this->belongsTo(Doctor::class);
}
}
型号地址
class Address extends Model
{
public function doctors() {
return $this->hasMany(Address::class);
}
}
医生控制器
class DoctorsController extends Controller
{
public function index()
{
$doctors = Doctor::with('addresses')->get();
return view('doctors.index',compact('doctors'));
}
}
刀
@foreach($doctors as $doctor)
{{ $doctor->name }}
@foreach($doctor->addresses as $address)
{{ $address->city }}
@endforeach
@endforeach
我有一个错误
为 foreach() 提供的参数无效
我试图在 Doctor 和 Address 之间建立关系,但它不起作用。如果我尝试 dd($doctor->addresses) 我有空值。