在laravel中,我将当前用户视为自己的追随者,而不是追随者

时间:2016-06-30 18:45:55

标签: laravel-5

在数据库中,我有以下结构

id | following_id | followed_id
1  |     3        |    1
2  |     2        |    1

I have user model and it looks like this

public function userFollows() { return $this->belongsToMany('Diskourse\Models\User','followers','following_id','followed_id');

}

public function userIsFollowed()
{
    return $this->belongsToMany('Diskourse\Models\User','followers','followed_id','following_id');
}

public function followers()
{
   return $this->userIsFollowed->merge($this->userFollows);
}

And at last view looks like this

<h4>Followers</h4> @if(!$user->followers()->count()) <p>No followers</p> @endif @foreach($user->followers() as $follower) @include('user/partials/userblock') @endforeach </div> </div>

如果它工作,它应该显示用户3和2作为1的跟随者。而是当前用户配置文件块显示两次。请帮助!!!!

1 个答案:

答案 0 :(得分:0)

exit()部分寻找变量userblock吗?因为仍然设置为当前用户。我会用这个:

$user

这将重复每个关注者的@each('user.partials.userblock', $user->userFollows, 'user') 视图,将关注者传递为userblock