与拉拉维尔的友谊体系:多对多关系


Friendship system with Laravel : Many to Many relationship

我正试图用Laravel创建一个友谊系统(我从它开始(,但我被关系阻塞了。事情是这样的:有一个表Users和一个表Friends,其中包含以下列:

friends: id, user_id, friend_id, accepted.

它看起来像是一个多对多,所以下面是我在用户类上设置的内容:

class User extends Eloquent {
    function friends()
    {
        return $this->belongsToMany('User');
    }
}

但当我尝试时

$friends = User::find($id)->friends()->get()

我有这个错误:

Base table or view not found: 1146 Table 'base.user_user' doesn't exist

我想得到一个用户的好友列表,不管用户是发送邀请还是收到邀请。所以用户可以在user_id或friend_id上ba,然后我根据该列检索其他用户的数据。

知道吗?谢谢!

编辑:这是我使用的代码:

$usersWithFriends = User::with('friendsOfMine', 'friendOf')->get();
$user = User::find(Auth::id())->friends;
foreach($user as $item) {
    echo $item->first()->pivot->accepted;
} 

tldr;您需要两个反向关系才能使其工作,请检查下面的SETUP和USAGE


首先是错误-这就是您的关系应该是什么样子的:

function friends()
{
  return $this->belongsToMany('User', 'friends', 'user_id', 'friend_id')
    // if you want to rely on accepted field, then add this:
    ->wherePivot('accepted', '=', 1);
}

然后它将在没有错误的情况下工作:

$user->friends; // collection of User models, returns the same as:
$user->friends()->get();

设置

但是您希望该关系以两种方式工作。Eloquent不提供这种关系,因此您可以使用2个反向关系并合并结果

// friendship that I started
function friendsOfMine()
{
  return $this->belongsToMany('User', 'friends', 'user_id', 'friend_id')
     ->wherePivot('accepted', '=', 1) // to filter only accepted
     ->withPivot('accepted'); // or to fetch accepted value
}
// friendship that I was invited to 
function friendOf()
{
  return $this->belongsToMany('User', 'friends', 'friend_id', 'user_id')
     ->wherePivot('accepted', '=', 1)
     ->withPivot('accepted');
}
// accessor allowing you call $user->friends
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))
    {
        $friends = $this->mergeFriends();
        $this->setRelation('friends', $friends);
    }
}
protected function mergeFriends()
{
    return $this->friendsOfMine->merge($this->friendOf);
}

用法

有了这样的设置,你可以做到这一点:

// access all friends
$user->friends; // collection of unique User model instances
// access friends a user invited
$user->friendsOfMine; // collection
// access friends that a user was invited by
$user->friendOf; // collection
// and eager load all friends with 2 queries
$usersWithFriends = User::with('friendsOfMine', 'friendOf')->get();
// then
$users->first()->friends; // collection
// Check the accepted value:
$user->friends->first()->pivot->accepted;

这可能是数据库和关系定义中的一个问题。多对多关系类型希望您使用和中间表。以下是你必须做的:

  1. 在架构中创建一个user_friend (id, user_id, friend_id)
  2. userfriend表中删除不必要的字段
  3. 创建正确的外键。user.id->user_friend.user_idfriend.id->user_friend.friend_id
  4. 用户朋友模型上更好地定义完全关系

例如:

 class User extends Eloquent {
    function friends()
    {
        return $this->belongsToMany('User', 'user_friend', 'user_id', 'friend_id');
    }
}

您可以在Laravel文档中阅读更多信息,此处