Reputation: 6394
I've come across a problem with my Eloquent model where having multiple relationships to the same table results in the first working but subsequent relationships return null, despite the data existing.
The relationship on created_by
works, however the completed_by
does not, despite it referencing the same data.
public function completed_by_user()
{
return $this->hasOne('User', 'id', 'completed_by');
}
public function user()
{
return $this->hasOne('User', 'id', 'created_by');
}
Above are the relationships within my Model of which $object->user
returns a value, however $object->completed_by_user
does not.
Am I missing something completely stupid?
Thanks and happy new year :)
Upvotes: 0
Views: 85
Reputation: 152860
You have to change the name of the relationship from completed_by_user
to completedByUser
The reason behind this is that Laravel converts the dynamic property name to camel case.
So when you try to use $user->completed_by_user
it searches for the relation function completedByUser
Upvotes: 1
Reputation: 6394
It seems that by calling
{{ $question->load('completed_by_user') }}
that it started working correctly...
Upvotes: 0
Reputation: 81157
Your relations are wrong, swap the keys:
public function completed_by_user()
{
return $this->hasOne('User', 'completed_by', 'id'); // id may be ommited
}
public function user()
{
return $this->hasOne('User', 'created_by', 'id'); // the same here
}
Upvotes: 0