Change
ContactNotes::find($note_id)
to
ContactNotes::with('note_author')->find($note_id)
and that should work.
Thanks Matthew, to be honest, I didn't got it working that way.. However, I got it working another way:
public function user(){
return $this->belongsTo('User');
}
I just renamed the method to user, like the name of the model, which actually is more consistent.
The problem was that you didn't specify the foreign key while method name was not sticking to the convention.
Using user
is sticking to the convention, so it works, but to make it clear:
// Your first attempt was equivalent of:
public function note_author()
{
return $this->belongsTo('User', 'note_author_id', null, 'note_author');
}
// signature:
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null) { }
// while you have on your table field 'user_id', so you could do this and it would work too:
public function note_author()
{
return $this->belongsTo('User', 'user_id');
}
Replace
$note->note_author->display_name
with
$note->note_author()->display_name
Of course not ;)
Read about dynamic properties as what you suggest will fail with undeclared property display_name
on the Relation
Albert221 said:
Replace
$note->note_author->display_name
with
$note->note_author()->display_name
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community