下面的代码运行,而另一个则不运行。代码在foreach循环中运行。 有谁知道为什么第一个没有运行?
{{ Status::find($workorder->statuses_id)->name }} //this doesn't
{{ Status::find(1)->name }} //this works
答案 0 :(得分:0)
假设您的关系定义如下......
class Workorder extends Eloquent {
public function status() {
return $this->hasOne('Status');
}
}
您需要这样做:
{{ Status::find($workorder->status->id)->name }}
这对我没有任何问题。
或者,如果您想使用最初提供的语法,可以在Workorder类上定义一个方法,如下所示:
public function getStatusesIdAttribute() {
return $this->hasOne('Status')->first()->id;
}
......但这有点尴尬,可能不是最好的方法。