首页 文章

Laravel从两个连接多个关系的表中获取数据

提问于
浏览
1

伙计我首先有2个表是 users 第二个是 notes

我用 Many to many relationship 连接两个表

users,notes和user_note表

See the 3 tables image

现在我试图登录用户注释,我已经通过 Auth::user() -> id 登录用户ID,我也试图获得 all the other users ,可以看到相同的注释 .

例如

user_id=1 可以看到 note_id=2user_id=2 可以看到 note_id=2 .

所以 note_id=22 users .

问题:

我想雄辩,让我所有登录用户的笔记和其他共享相同笔记的用户 .

我试过 whereHas 但是我无法得到我需要的东西 .

1 回答

  • 0

    试试这个:

    $loggedInUser = Auth::user();
    $notes = $loggedInUser->notes()->with('users')->get();
    

相关问题