- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试使用 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,然后我根据该列检索其他用户的数据。
有什么想法吗?谢谢!
编辑:这是我使用的代码:
$usersWithFriends = User::with('friendsOfMine', 'friendOf')->get();
$user = User::find(Auth::id())->friends;
foreach($user as $item) {
echo $item->first()->pivot->accepted;
}
最佳答案
首先是错误 - 你的关系应该是这样的:
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;
关于php - Laravel 的友谊系统 : Many to Many relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25049753/
我正在尝试在 laravel 5 中建立一个友谊系统,但我被卡住了。我有一个 friends_user 表,如下所示: 编号 用户编号 friend_id 状态 重点是,我转到一个用户页面,我想看看我
很多人说我应该创建一个友谊表(userid , friendid)。 我的问题是: 每一段友谊不会有双重记录吗?喜欢: 12 是 5 的 friend + 5 是 12 的 friend 有什么办法可
我是第一次尝试使用 ORM,我定义了这样的模型: 用户模型: class User extends Eloquent { public function friends() {
您将如何塑造 friend -Grails中的友谊关系?到目前为止,我的User类有很多关注者 class User { //Searchable plugin static searchable =
我尝试在 Instagram 中建立友谊/创建/运作,但我遇到了一些错误。 账户/登录功能正常。 friendships/create 错误:“请更新您的 Instagram 应用以继续关注。” re
在本页https://dev.twitter.com/rest/reference/post/friendships/destroy你可以读到 POST 友谊/破坏是速率限制的。 但是这个其他页面ht
我是一名优秀的程序员,十分优秀!