Reputation: 3440
So I Have this very large search query and I am trying to also remove any users from the search who have blocked the user searching.
I am getting really confused and turned around and would love any assistance.
The blocked_users table contains
id
user_id
blocked_user_id
when a user blocks another user a row is created. Now So far I am noticing if a user has not blocked anyone they are not showing up in the search at all (my guess because of the join, if the user does not exist in the table then they arnt showing up??.)
here is a short snippet of my query
$query = User::join('user_profiles', 'users.id', '=','user_profiles.user_id');
$query->Join('blocked_users', 'users.id', '=', 'blocked_users.user_id');
$query->whereNotIn('blocked_users.blocked_user_id',[Auth::user()->id])->select('users.*','user_profiles.*');
EDIT
Ok so If my user id is 1, and user 2 does not wish for me to contact them any more or even show up in my search, They would block me which would add a row in a table
int, user_id=2, blocked_user_id=1
If i Do a search I dont want user 2 showing up in user 1s search.
However with the leftJoin No users are showing up if my user id is in any row of the blocked_user_id regardless of the user who blocked me.
I am not to sure how else I could give visual data. the whole query is quite massive and works perfectly. I am just not sure how to combine the where clause.
Upvotes: 0
Views: 466
Reputation: 424
This might do it:
$query = User::join('user_profiles', 'users.id', '=','user_profiles.user_id');
$query->leftJoin('blocked_users', function ($sub_query) {
$sub_query->on('users.id', '=', 'blocked_users.user_id');
$sub_query->where('blocked_users.blocked_user_id', '=', Auth::user()->id);
});
$query->where('users.id', '!=', Auth::user()->id);
$query->whereNull('blocked_users.id');
$query->select('users.*','user_profiles.*');
Upvotes: 1