沃梦达 / 编程问答 / php问题 / 正文

与 Laravel 的友谊系统:多对多关系

Friendship system with Laravel : Many to Many relationship(与 Laravel 的友谊系统:多对多关系)

本文介绍了与 Laravel 的友谊系统:多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a Friendship system with Laravel (I'm starting with it) but I'm blocked with relationships. Here's the thing : there is one table Users and one table Friends which contains the following columns :

friends: id, user_id, friend_id, accepted.

它看起来像多对多,所以这是我在用户类上设置的:

It looks like a Many to Many so here's what I set on User class :

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 获取数据,然后根据该列检索其他用户的数据.

I would like to get a list of the Friends of a user, no matters if the user sent the invitation or received it. So the user can ba on user_id or on friend_id and then I retrieve the data of the other user depending of that column.

有什么想法吗?谢谢!

这是我使用的代码:

$usersWithFriends = User::with('friendsOfMine', 'friendOf')->get();
$user = User::find(Auth::id())->friends;

foreach($user as $item) {
    echo $item->first()->pivot->accepted;
} 

推荐答案

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

<小时>

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

tldr; you need 2 inverted relationships to make it work, check SETUP and USAGE below


First off the error - this is how your relation should look like:

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 个反向关系并合并结果:


SETUP

However you would like the relation to work in both ways. Eloquent doesn't provide a relation of that kind, so you can instead use 2 inverted relationships and merge the results:

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

用法

通过这样的设置,您可以做到:

USAGE

With such setup you can do this:

// 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;

这篇关于与 Laravel 的友谊系统:多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:与 Laravel 的友谊系统:多对多关系

基础教程推荐