我质疑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
不会生效,并且将继续显示以前的帖子。 / p>
答案 0 :(得分:1)
只要tag_id
在给定集中,或者user_id
在给定集中,无论哪种情况,似乎都希望获取结果。没有给定的集合。在这种情况下,您需要对post_id
和tag_id
条件进行分组。
user_id
这等效于在原始SQL中将它们放在括号中。您可以在“关系后的束缚或子句中”下了解此here。