【问题标题】:Followers / Following and finding who follows Laravel追随者/关注并查找关注 Laravel 的人
【发布时间】:2016-06-22 14:32:42
【问题描述】:

我的关系表如下:

UserId (PK)
FollowingId
created_at
updated_at

我可以通过执行以下操作来获取用户正在关注的人及其用户名:

我的控制器:

$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

然而,我正在努力做相反的事情,例如:获取关注我的帐户用户名的用户。我认为这是我的人际关系问题,我遗漏了一些明显的东西。

【问题讨论】:

标签: php mysql laravel eloquent


【解决方案1】:

按照建议的重复 Fabio Antunes (Friendship system with Laravel : Many to Many relationship) 修复我的关系来修复它

【讨论】:

    猜你喜欢
    • 2017-12-08
    • 2016-11-23
    • 2019-07-03
    • 2012-04-07
    • 1970-01-01
    • 2016-06-12
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多