Closed sarik-k closed 2 years ago
Use the model that the custom foreign key is referencing (instead of the model/table the key is in):
public function user()
{
return $this->belongsToThrough(
'App\Models\User',
'App\Models\Post',
null,
'',
['App\Models\User' => 'added_by']
); ^^^^^^^^^^^^^^^
}
I have a
hasManyThrough
relationship like this:user
→ has many →post
→ has many →comments
The post has a column
added_by
which referencesid
onusers
.My relationships are like this:
User.php
Comment.php
However when running
$comment->user
, I'm getting the following error:It looks like its still trying to find
user_id
column inposts
table in the lastinner join
statement instead ofadded_by
.