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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -