laravellaravel-5laravel-8

get linked data from user table in laravel


I have this linkUser Table

id |  User Id |   LinkedUser ID


1        4            6
2        6            4
3        1            6
4        6            1
5        1            2
6        6            3
7        3            6

and then there is user table

id  |  name  
1      user 1
2      user 2
3      user 3
..
..

I want to get all user detail where current user id is linked to other user id. Like if current logged in user id is 6 then it should return data 4,1,3 data from user table

I'm trying like this

DB::table('linked')->where('user_id', $loggedUserId)->get();

enter image description here


Solution

  • This should return all the user details where current logged-in user id is linked to other user id :

    $users = DB::table('users')
        ->join('linkUser', 'users.id', '=', 'linkUser.user_id')
        ->where('linkUser.linked_user_id', $loggedUserId)
        ->select('users.*')
        ->get();