Laravel accessing one to one relationship from the views -
the code below 1 runs , other doesn't. code runs inside foreach loop. know why first 1 doesn't run?
{{ status::find($workorder->statuses_id)->name }} //this doesn't {{ status::find(1)->name }} //this works
assuming have relationship defined this...
class workorder extends eloquent { public function status() { return $this->hasone('status'); } }
you need do:
{{ status::find($workorder->status->id)->name }}
this works me no issues.
alternatively, if want use syntax provided define method on workorder class this:
public function getstatusesidattribute() { return $this->hasone('status')->first()->id; }
...but little awkward , not best approach.
Comments
Post a Comment