【问题标题】:Laravel 5.* - Eloquent Many to Many RelationshipsLaravel 5.* - 雄辩的多对多关系
【发布时间】:2017-07-12 10:01:35
【问题描述】:

我有以下表格

USERS = username | email | name 
FOLLOWERS = user_id | follower_id

当登录用户点击“关注”时,我的代码将他的 id 保存在 followers.follower_id 中,而他想要关注的用户的 id 保存在 followers.user_id 中。

要查看用户拥有多少关注者以及用户关注了多少用户,我使用:

$followers = Follower::where('user_id', $user->id)->count();
$following = Follower::where('follower_id', $user->id)->count();

这很好用,但我想显示有关一位用户的关注者的信息。我尝试了以下方法:

$first_follower = $followers[0]->user->username;

但它返回的是用户关注而不是关注者。

我想知道如何获取关注者的信息

用户模型

protected $fillable = ['username','email','name'];

public function follow() {
  return $this->hasMany('Shop\Follower');    
}

追随者模型

protected $fillable = ['user_id','follower_id'];

public function user() {
  return $this->belongsTo('Shop\User');
}

【问题讨论】:

  • 你应该使用多对多关系来克服这个问题

标签: php laravel eloquent foreign-keys many-to-many


【解决方案1】:

上面是您应该如何实现关系的示例。通过关注者表,许多用户对许多用户都是如此。您可能不需要追随者模型,因为您已经有了用户模型。经过一些分析和改进后,您的代码将可以正常工作,但我强烈建议您改用这种 inuser 模型:

public function followers() { return $this->belongsToMany('App\User','followers','user_id','follower_id'); } public function following_users() { return $this->belongsToMany('App\User','followers','follower_id','user_id'); }

您可以访问关注者 $user->followers(这将返回 eloquent 集合,您将能够根据 laravel 文档 collection api 做任何您想做的事情)和某个像 $user->followers[0] 的人

希望我能正确回答。

【讨论】:

    【解决方案2】:

    如果我做对了,追随者是用户类/模型的实例,所以你不需要追随者模型。你可以定义一个Many To Many Relationship

    在您的用户模型中,您可以添加:

    public function followers()
    {
        return $this->belongsToMany('Shop\User', 'followers', 'user_id ', 'follower_id');
    }
    
    public function following()
    {
        return $this->belongsToMany('Shop\User', 'followers', 'follower_id', 'user_id');
    }
    

    您可以通过$user->followers 访问用户关注者,这将返回一个Laravel Collection,使用$user->following 您可以访问用户关注的关注者。

    //Get the count of all followers for $user
    $count = $user->followers()->count();
    
    //Get the count of all followed by $user
    $count = $user->following()->count();
    
    //Get the username of the first follower
    $follower = $user->followers()->first();
    echo $follower->username;
    
    //Loop trough all followers
    foreach ($user->followers as $follower) {
         echo $follower->username;
    }
    

    定义这种关系可以帮助您保存/删除关注者,只需使用attach()detach() 方法

    // The $user will be followed by an user with $followerId
    // A record in the `followers` table will be created with
    // user_id = $user->id and follower_id = $followerId
    $user->followers()->attach($followerId);
    
    // The $user will stop be followed by an user with $followerId
    $user->followers()->detach($followerId);
    

    附注: 调用followers() 方法和调用followers 属性是有区别的。第一个将返回 BelongsToMany 关系,您可以在其上调用所有 Eloquent 查询构建器方法,后者将返回 Collection

    /** @var Illuminate\Support\Collection */
    $user->followers;
    
    /** @var Illuminate\Database\Eloquent\Relations\BelongsToMany */
    $user->followers();
    

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 2015-04-28
      • 2016-11-03
      • 2013-01-22
      • 1970-01-01
      • 2012-10-08
      • 2018-10-27
      • 2018-03-28
      • 1970-01-01
      相关资源
      最近更新 更多