In my Message.php
model, I defined the following:
class Message extends Eloquent {
public function userfrom()
{
return $this->hasOne('User', 'id', 'userfrom');
}
}
In my in-app message system, I want to be able to simply retrieve the users.username
column where users.id = messages.userfrom
. I tried Message::find(1)->userfrom()->username
and it throws me an error, that the $username
property is undefined.
Any idea what's going wrong?
Test with:
Message::find(1)->userfrom()->first()->username;
//or
Message::find(1)->userfrom->username;
Works with Message::find(1)->userfrom()->first()->username;
, thanks.
I figured why it didn't work with Eloquent's dynamic properties: I named the method after the column (userfrom
), so $message->userfrom
returned the actual column and didn't called the relationship method.
I renamed it from()
and now Message::find(1)->from->username
works well!
Great
Yeah, Relationship names have be different of properties.
Edit the post and put as Solved.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community