2016-06-30 24 views
0

在数据库的我终于视图结构如下在laravel,我得到当前用户的视图作为自身的一个追随者,而不是追随者

 
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); 
} 

,看起来像这样

<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的跟随者,而不是当前用户配置文件块显示两次。请帮忙 !!!!

回答

0

userblock部分寻找变量$user?因为这仍然是给当前用户的。我会用这样的:

@each('user.partials.userblock', $user->userFollows, 'user') 

这将重复userblock查看每个追随者,通过跟随在为$user

+0

发生错误说“关系方法必须返回类型的对象”,而使用$用户>追随者作为追随者 – pandesantos

+0

为什么你在followers()关系中合并追随者和userIsFollowed集合?确定看到编辑,我不明白你的关系,使用'userFollows'应该可以工作 – Jeff

+0

好吧我已修改关系从上面到这个 公共职能追随者() { return $ this-> belongsToMany('Diskourse \ Models \用户','追随者','follow_id','user_id') - > get(); }我仍然面临同样的问题 – pandesantos