問題描述
我正在嘗試使用 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 的友誼系統:多對多關系的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!