Laravel `orWhereIn` 使 `whereNotIn` 不生效

Laravel `orWhereIn` is making `whereNotIn` not take effect

我查询 orWhereIn 使 whereNotIn 没有生效,但我没有得到想要的结果!这两个的专栏不同。

当我更改这些查询片段的顺序时,有时它只重复最后一行,有时重复最后 3 行,有时结果出乎意料

$userposts = userPost::with([
    //RELATIONS--------------------------------------------------------------------------
    'getUser.userbaseinfo',
    'getUser.usercertinfo',
    'getUser.getJobexp',
    'getLike'                           => function ($q) {
        $q->where('liked', 1);
        $q->orderByRaw("FIELD(user_id ," . auth()->user()->id . ") DESC");
    },
    'getLike.getUser.Userbaseinfo',
    'getLike.usercertinfo.getmajor',
    'getLike.usercertinfo.getuniversity',
    'getLike.userjobexp.getCompany',
    'getLike.getcandidate',
    'getLike.userbaseinfo',
    'gettags',
    'getCandidate.getBaseinfo',
    'getCandidate.getCertificate.getmajor',
    'getCandidate.getCertificate.getuniversity',
    'getCandidate.candidjobexp.getCompany',
    'getComments'                       => function ($q) {
        $q->orderByRaw("FIELD(user_id ," . auth()->user()->id . ") DESC");
    },
    'getComments.userbaseinfo',
    'getComments.getcandidate',
    'getComments.usercertinfo.getmajor',
    'getComments.usercertinfo.getuniversity',
    'getComments.userjobexp.getCompany',
    'getfriendreq'                      => function ($q) {
        $q->where('requester_id', auth()->user()->id);
    },
    'getfollow'                         => function ($q) {
        $q->where('req_id', auth()->user()->id);
    },
    'getComments.getLike'               => function ($q) {
        $q->orderByRaw("FIELD(user_id ," . auth()->user()->id . ") DESC");
        $q->where('liked', 1);
    },
    'getComments.getLike.getcandidate',
    'getComments.getLike.getuser',
    'getComments.getLike.Userbaseinfo',
    'getComments.getLike.usercertinfo',
    'getComments.getLike.Userjobexp'    => function ($q) {
        $q->limit(1);
    },
    'getsharedpost.getUser.userbaseinfo',
    'getsharedpost.getUser.usercertinfo',
    'getsharedpost.getUser.getJobexp',
    'getsharedpost.getCandidate',
    'getComments.childcomments'         => function ($q) {
        $q->orderByRaw("FIELD(user_id ," . auth()->user()->id . ") DESC");
    },
    'getComments.childcomments.userjobexp',
    'getComments.childcomments.getcandidate',
    'getComments.childcomments.usercertinfo',
    'getComments.childcomments.userbaseinfo',
    'getComments.childcomments.getLike' => function ($q) {
        $q->orderByRaw("FIELD(user_id ," . auth()->user()->id . ") DESC");
        $q->where('liked', 1);
    },
    'getComments.childcomments.getLike.getuser',
    'getComments.childcomments.getLike.userjobexp',
    'getComments.childcomments.getLike.getcandidate',
    'getComments.childcomments.getLike.usercertinfo',
    'getComments.childcomments.getLike.userbaseinfo'])
    //END OF RELATION----------------------------------------------------------------

    //If I change these query piece order the result might be changed
    ->whereHas('gettags', function ($q) use ($TagsToLook) {
        $q->whereIn('tag_id', $TagsToLook);
    });

$userposts = $userposts->orWhereIn('user_id', $Sourceloader)->where('user_id', auth()->user()->id);

if (count($lastpostid) > 0) {
    $userposts = $userposts->whereNotIn('post_id', $lastpostid);
}

$result = $userposts->orderBy('created_at', 'desc')->limit(3)->get();

想要的 result:showing 帖子与尚未显示的关系 Where 它不是登录用户 orWhere user_id 等于 $sourceloader.

实际结果:如果whereNotIn($lastpostid)中的帖子来自于ID在$sourceloader中的用户,则whereNotIn不会生效,它会继续显示以前的帖子.

看起来您想要的是获取 tag_id 在给定集中或 user_id 在给定集中的结果,只要在任一情况下 post_id 不在给定的集合中。在这种情况下,您需要对 tag_iduser_id 条件进行分组。

->where(function($query) use ($TagsToLook) {
  return $query
    ->whereHas('gettags', function ($q) use ($TagsToLook) {
      $q->whereIn('tag_id', $TagsToLook);
    })
    ->orWhereIn('user_id', $Sourceloader);
});

这相当于将它们放在原始的括号中 SQL。您可以在 "Chaining orWhere Clauses After Relationships".

下阅读此 here