Laravel&雄辩 - 查询“朋友”关系 [英] Laravel & Eloquent - Query 'Friends' Relationship

查看:215
本文介绍了Laravel&雄辩 - 查询“朋友”关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说你有用户和朋友模式

  user:
- id
- username

朋友:
- id
- user_id
- friend_id

友谊由两个相互记录决定。让我们说如果它只是一种单向关系,这是一个待处理的请求。所以在朋友表中:

  user_id:5 | friend_id:6 
user_id:6 | friend_id 5

在用户模型中,您将拥有:

  public function friends()
{
return $ this-> belongsToMany(self :: class,'friends','user_id' 'friend_id') - > withTimestamps();
}

但是,这不是整个故事,因为这不检查相互关系的存在。



你将如何设置? friend()方法应该负责进一步查询数据吗?或者这种方法是否合适,而您希望获得相互友谊的实例,则应该附加一个查询范围?例如

  $ this-> user-> friends-> mutual()

  $ this-> ; user-> friends-> pending()


解决方案

这个答案,结束了这一切。但是,我将 merge()功能更改为 intersect(),因为我没有使用批准标志来确定天气的友谊是相互的。我只是使用两种关系的存在。



所以在我的情况下。关系是:

  public function myFriends()
{
return $ this-> belongsToMany自::类, '朋友', 'USER_ID', 'friend_id') - > withTimestamps();
}

public function friendsOf()
{
return $ this-> belongsToMany(self :: class,'friends','friend_id','user_id ) - > withTimestamps();
}

其他逻辑是:

  public function getFriendsAttribute()
{
if(!array_key_exists('friends',$ this-> relationships))$ this-> loadFriends();

return $ this-> getRelation('friends');
}

protected function loadFriends()
{
if(!array_key_exists('friends',$ this->关系))$ this-> setRelation ('friends',$ this-> mergeFriends());
}

保护函数mergeFriends()
{
返回$ this-> myFriends-> intersect($ this-> friendsOf);
}


Lets say you have User and Friend Schemas

user:
   - id
   - username

friends:
   - id
   - user_id
   - friend_id

A 'friendship' is determined by having two reciprocal records. And lets says if it's just a one way relationship, this is a pending request. So in the friends table:

user_id: 5 | friend_id: 6
user_id: 6 | friend_id 5

In the User Model, you would then have:

public function friends()
    {
        return $this->belongsToMany(self::class,'friends','user_id','friend_id')->withTimestamps();
    }

However, this isn't the whole story because this doesn't check for the presence of the mutual relationship.

How would you set this up? Should the friends() method be responsible for for querying the data further? Or is this method appropriate, and instances where you want to get a mutual friendship, you should append a query scope? For example

$this->user->friends->mutual()

OR

$this->user->friends->pending()

解决方案

Ended up figuring this out based on this answer. However, i changed the merge() functionality to intersect() because I'm not use the approved flag to determine weather the friendship is mutual. I'm only using the presence of the two relationships.

So in my case. the relations are:

public function myFriends()
{
    return $this->belongsToMany(self::class,'friends','user_id','friend_id')->withTimestamps();
}

public function friendsOf()
{
    return $this->belongsToMany(self::class,'friends','friend_id','user_id')->withTimestamps();
}

And the other logic is:

public function getFriendsAttribute()
{
    if ( ! array_key_exists('friends', $this->relations)) $this->loadFriends();

    return $this->getRelation('friends');
}

protected function loadFriends()
{
    if ( ! array_key_exists('friends', $this->relations)) $this->setRelation('friends', $this->mergeFriends());
}

protected function mergeFriends()
{
    return $this->myFriends->intersect($this->friendsOf);
}

这篇关于Laravel&雄辩 - 查询“朋友”关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆