Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

Change

ContactNotes::find($note_id) 

to

 ContactNotes::with('note_author')->find($note_id) 

and that should work.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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');
}
Last updated 2 years ago.
0

Replace

$note->note_author->display_name

with

$note->note_author()->display_name
Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

pixionnl pixionnl Joined 16 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.