skip to Main Content

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

2

Answers


  1. 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();
    
    Login or Signup to reply.
  2. May be this will help, you can try this.

    $loggedUserId = 6; // Make sure you getting "6"
    
    DB::table('user u')
    ->leftjoin('linked l', 'l.user_id', '=', 'u.id')
    ->where('u.user_id', $loggedUserId)
    ->select('u.*')
    ->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search