我与雄辩的关系混淆。我有两个表:Laravel用户与朋友的雄辩关系
USERS:
id, name, pwd, etc...
FRIENDS:
id, user_id, friend_id
对于FRIENDS表,我有两个外键:
$t->foreign('user_id')
->references('id')
->on('users');
$t->foreign('friend_id')
->references('id')
->on('users');
在每一节课,我有以下关系:
USER类:
/**
* A user can have many friends
*/
public function friends()
{
return $this->hasMany(User::class, 'friend_id', 'id');
}
FRIEND类别:
/**
* A friend can belong to a user
*/
public function friend()
{
return $this->belongsTo(User::class, 'id', 'user_id');
}
/**
* A user friends are connected to
*/
public function user()
{
return $this->belongsTo(User::class, 'id', 'friend_id');
}
我想
Auth::user()->friends()->get();
落得应的关系是hasManyThrough()
?如何在USERS表通过FRIENDS表自引用时实现这一点?
首先,为什么你在朋友表 – Didi
@Didi一个friend_id,其支点。我敢打赌,他只需要删除数据透视表中的'id'。 @TheRealPapa,我不认为你需要'hasManyThrough()',因为这是一个直接的多对多关系。 –
是的,你不需要hasManyThrough()。 – Didi