首页 > 解决方案 > Laravel `orWhereIn` 使 `whereNotIn` 不生效

问题描述

我的查询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();

期望的结果:显示未显示关系的帖子,Where其不是已登录的用户orWhereuser_id 等于 $sourceloader。

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

标签: laraveleloquentlaravel-query-builder

解决方案


看起来您想要的是获取tag_id在给定集合中或user_id在给定集合中的结果,只要在任何一种情况下post_id都不在给定集合中。在这种情况下,您需要对tag_idanduser_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 or Where Clauses After Relationships”下阅读有关此内容的内容。


推荐阅读