【发布时间】: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