我的关系表如下:
UserId (PK)
FollowingId
created_at
updated_at
我可以通过执行以下操作来获取用户正在关注的人及其用户名:
我的 Controller :
$findingWhoIFollow = User::find($userId)->relations()->FindUserName()->get();
我的模型(用户):
public function relations()
{
return $this->hasMany(Relation::class, 'UserId', 'id');
}
我的模型:(关系)
protected $primaryKey = 'UserId';
public function user()
{
return $this->belongsToMany(User::class, 'id', 'UserId');
}
public function scopeFindUserName($query)
{
return $query->join('User', 'User.id', '=', 'Relation.FollowingId');
}
然后我就可以获取我关注的人的用户名,如下所示:
@foreach($findingWhoIFollow as $iFollow)
{{$iFollow->Username}}
@endforeach
然而,我正在努力做相反的事情,例如:获取关注我帐户用户名的用户。我认为这是我的人际关系问题,而且我遗漏了一些明显的东西。
最佳答案
通过按照 Fabio Antunes 发布的建议重复内容修复我的关系来修复此问题 ( Friendship system with Laravel : Many to Many relationship )
关于php - 关注者/关注并查找关注 Laravel 的人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35870893/