-1

There are three tables: users, profiles, friend_request

profiles table's column : profile_id, id (foreign key with users) , first_name,last_name

friend_request's table column : request_id, sender_id,to_user_id, request_status

logic : if profile_id exist in either sender_id column or to_user_id of friend_request table with request_status 2 then they are friends.

example : sender_id to_user_id request_status

           5          6            2

$request_accept = DB::table('friend_request')->select('profiles.profile_id','profiles.first_name',
'friend_request.request_id')->leftjoin('profiles', function($join)
 {
   $join->on('friend_request.sender_id' , '=','profiles.profile_id');
   $join->orOn('friend_request.to_user_id' , '=','profiles.profile_id');
  }
 )->where('to_user_id',$my_profile_id)->orwhere('sender_id',$my_profile_id)->where('interest_status','2')->whereNotIn('profiles.profile_id', function($q) use ($my_profile_id)
  {
   $q->select('profile_id')->from('profiles')->where('profile_id',$my_profile_id);
   })->groupby('profiles.profile_id')->get(); 

wherenotin i snot working.

Entrepreuner
  • 53
  • 4
  • 11

1 Answers1

1

If I understand what you are asking...

Probably better to make these all models, with relationships, then you could do a much simpler query, for example like this.

Assume your friend_request table now has a FriendRequest model with a user relationship. The query below uses a $userId, assume this is the ID for the user whom you wish to get all the friend requests.

FriendRequest::where('to_user_id', $my_profile_id)->get();

If everything is set up correctly, the user will not be returned as they cannot send themselves a friend request.

John Halsey
  • 1,930
  • 3
  • 19
  • 41