【发布时间】:2021-04-27 11:20:06
【问题描述】:
我有 3 个模型 User、Follow 和 Post
这些关系是这样的:
User模特:
public function userPosts()
{
return $this->hasMany('App\Model\User\Post');
}
public function follows()
{
return $this->hasMany('App\Model\User\Follow','user_id');
}
public function fans()
{
return $this->hasMany('App\Model\User\Follow','follow_id');
}
Post 型号
public function user()
{
return $this->belongsTo('App\User');
}
Follow模特
public function user()
{
return $this->belongsTo('App\User');
}
public function user_follow()
{
return $this->belongsTo('App\User','follow_id');
}
每个帖子都属于某个用户。我有一个关注列表,其中用户有关注用户。在关注模型中有两列,user_id关注的用户和关注用户的follow_id。
现在我想在新闻源中显示帖子我想显示我关注的用户的帖子和我的帖子。
我如何向他们展示 Laravel 雄辩的关系 我正在使用 Laravel 7.2
【问题讨论】:
标签: php mysql laravel eloquent-relationship