我正在Laravel 5.2中建立跟随关系,我在用户模型中定义了关系,这是我的代码。
public function iamfollowing()
{
return $this->belongsToMany('Diskourse\Models\User','followers','user_id','followed_id');
}
public function iamfollowedBy()
{
return $this->belongsToMany('Diskourse\Models\User','followers','followed_id','user_id');
}
public function following()
{
return $this->iamfollowing()->get();
}
public function followers()
{
return $this->iamfollowedBy()->get();
}
但是当我尝试显示关注者列表时,它只显示当前用户等于预期关注者的数量。这是我的观点:
<div class="col-lg-4 col-lg-offset-3">
<!--Followers list-->
<h4>Followers</h4>
@if(!$user->followers()->count())
<p>No followers</p>
@endif
@foreach($user->followers() as $follower)
@include('user/partials/userblock')
@endforeach
</div>
</div>