我对雄辩的关系感到困惑。我有两张桌子:
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');
在每个班级中我都有以下关系:
用户类别:
/**
* A user can have many friends
*/
public function friends()
{
return $this->hasMany(User::class, 'friend_id', 'id');
}
朋友班:
/**
* 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 表自引用时,如何实现此功能?