我正在开发一个具有4个数据库表的Laravel 4应用程序:
我使用外键引用其他表中的数据。这些关系如下:
我用的是雄辩的ORM。
这是我尝试使用的代码:
治疗师模式:
public function therapistType() {
return $this->belongsTo('TherapistType');
}
public function municipality() {
return $this->hasOne('Municipality');
}
public function county() {
return $this->hasOne('County');
}
}市政模式:
public function county() {
return $this->hasOne('County');
}在我的控制器中,我使用以下代码来获取治疗师:
$therapists = Therapist::paginate(10);
return View::make('index', compact('therapists'));最后,在我看来,这就是我想为治疗师获得相应的治疗师类型的方法:
<span class="therapisttype">{{{ $therapist->therapistType }}}</span>但是我没有数据。
我做错了什么?
发布于 2015-02-07 23:57:12
$therapist->therapistType应该返回一个对象,但是您没有回显所述对象的属性。让我们想象一下therapistType表有一个name属性,那么您应该这样做
如果您想要重复这个名称,请使用{{$therapist->therapistType->name}}。
我从var_dumping (对象)开始,您可以使用$therapist->therapistType,假设您已经正确地设置了关系,您应该能够看到它的所有属性。
希望能帮上忙。
https://stackoverflow.com/questions/28388872
复制相似问题