简体   繁体   English

Laravel 5.2 WhereNotIn查询

[英]Laravel 5.2 WhereNotIn query

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 好的,如果我的用户ID为1,并且用户2不希望我再与他们联系甚至不希望出现在我的搜索中,则他们将阻止我,这将在表中添加一行

int, user_id=2, blocked_user_id=1 int,user_id = 2,blocked_user_id = 1

If i Do a search I dont want user 2 showing up in user 1s search. 如果我进行搜索,我不希望用户2出现在用户1s搜索中。

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. 但是,使用leftJoin时,无论谁阻止了我,无论我的用户ID在blocked_user_id的任何行中,都没有用户出现。

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. 我只是不确定如何结合where子句。

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.*');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM