【问题标题】:Laravel `orWhereIn` is making `whereNotIn` not take effectLaravel `orWhereIn` 正在使 `whereNotIn` 不生效
【发布时间】:2019-06-25 06:05:11
【问题描述】:

我有疑问 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 它不是登录用户orWhere user_id 等于 $sourceloader。

实际结果:如果whereNotIn($lastpostid)中的帖子来自id在$sourceloader的用户,那么whereNotIn不会生效,它会一直显示之前的帖子。

【问题讨论】:

    标签: laravel eloquent laravel-query-builder


    【解决方案1】:

    看起来您想要获取的结果是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 Relations”下阅读here

    【讨论】:

    • 只是为了澄清一下,如果我有几个确定的集合和另一个不确定的集合,我必须对某些集合进行分组?
    • 当我说“某个集合”时,我只是指您定义的集合。您需要对逻辑上组合在一起的条件进行分组,例如“(这个或那个)和这个”。
    • 哦,好吧,你的意思是因为没有if 声明,我们将它们组合在一起。具有相同逻辑的分组条件是有意义的。再次感谢
    猜你喜欢
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2020-05-02
    • 2018-05-17
    相关资源
    最近更新 更多