【发布时间】:2015-11-02 18:15:24
【问题描述】:
我正在尝试获取已设置的关系的 ID。
平板模型
public function plateContainer()
{
return $this->belongsTo('App\Models\PlateContainer');
}
PlateContainer模型
public function plate()
{
return $this->hasMany('App\Models\Plate');
}
public function equipmentStatusCode()
{
return $this->hasOne('App\Models\EquipmentStatusCode');
}
设备状态代码型号
public function plateContainer()
{
return $this->belongsTo('App\Models\PlateContainer');
}
当我尝试时。
$p = Plate::findOrFail(4);
$p->plateContainer->equipmentStatusCode;
return $p;
我收到Trying to get property of non-object。我做错了什么?
【问题讨论】:
-
plateContainer不是$p的属性,而是一种方法。这就是您收到错误的原因。 -
@Daan 我该如何解决这个问题?
-
指定表
plates和equipment_status_codes的表结构