这是我第一次使用hasOneThrough,我不太明白,我一定是在什么地方遗漏了一个hasOne。
当我发布我的评论时,我希望能够与用户进行连接,然后排名。
下面是用户模型之间的关系:
public function comments()
{
return $this->hasMany('App\Models\Comment');
}
public function rank()
{
return $this->belongsTo('App\Models\Rank');
}下面是模型Rank之间的关系:
public function users()
{
return $this->hasMany('App\Models\User');
}
public function user()
{
return $this->hasOne('App\Models\User');
}以下是模型注释之间的关系:
public function user()
{
return $this->belongsTo('App\Models\User');
}
===> public function rank()
{
return $this->hasOneThrough(Rank::class, User::class);
}我在控制器中的方法:
$comments = Comment::where('status','=', Comment::STATUS_VISIBLE)
->with('user') (working)
->with('rank') (not working)
->latest()
->paginate(10);提前感谢

发布于 2021-08-23 20:34:28
您可能正在寻找:
$comments = Comment::where('status','=', Comment::STATUS_VISIBLE)
->with('user')
->with('user.rank')
->latest()
->paginate(10);https://stackoverflow.com/questions/68898418
复制相似问题