laravel - 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
其不是已登录的用户orWhere
user_id 等于 $sourceloader。
实际结果:如果whereNotIn
($lastpostid) 中的帖子来自他们的 id 在 $sourceloader 中的用户,则whereNotIn
不会生效,它将继续显示以前的帖子。
解决方案
看起来您想要的是获取tag_id
在给定集合中或user_id
在给定集合中的结果,只要在任何一种情况下post_id
都不在给定集合中。在这种情况下,您需要对tag_id
anduser_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”下阅读有关此内容的内容。
推荐阅读
- java - 如何从网页Java中的多个URL中获取特定的URL
- java - Java 矩形程序
- node.js - 如何在 Android 上使用多合一 sdk 时将付款响应回调到我的后端
- arrays - 如何从json中的数组中获取数据?
- reactjs - 将特定子 div 与父状态数据进行比较
- python - 尝试适应 sklearn 模型时出现 ValueError
- awk - awk 命令从文件中读取键值对
- python - 为什么像数组这样的 Numpy 可变变量没有更新同一内存上的值?
- flutter - 我的 Firestore 规则访问其他用户路径时出现会议权限错误
- php - 如何对另一个wordpress php文件进行href?