【发布时间】: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