laravel - Laravel 中间关系数据错误
问题描述
这是我的RecentViewController文件代码:
$contents = RecentView::where('user_id', $loggedUser)
->with('posts')
->with('profile')
->paginate(12)->toArray();
我的RecentView模型代码如下:
public function posts()
{
return $this->hasOne('App\FeedPost', 'id', 'post_id');
}
public function profile()
{
return $this->belongsTo('App\Profile', 'user_id', 'id');
}
运行此代码后我收到的数据显示登录用户详细信息,而不是发布用户详细信息。如何更改检索帖子用户数据而不是记录用户数据?
解决方案
我猜您正在尝试获取帖子的用户为了获取帖子用户的详细信息,您需要将个人资料关系移动到Post
模型中。我希望有一个user_id
帖子表。然后用点符号得到后模型的轮廓关系如下
$contents = RecentView::where('user_id', $loggedUser)
->with('posts.profile')
->paginate(12)->toArray();