[英]How to do condition based query in Eloquent ORM, Laravel?
我想获得公开的时间表,可以访问所有公开的帖子,并让朋友获得所有的帖子(即可以是公开的也可以是私人的)。 我如何在一种情况下进行管理,即可以获得所有人的所有公开帖子和朋友的私人帖子。 以下代码仅适用于公共视频。
$friends = json_decode($this->friend->where('user', $this->currentUser->id)->first()->list, true);
$blockedUserArray = $this->getBlockedList();
$friends[] = $this->currentUser->id;
$videos = $this->video
->withCount('videoComment', 'videoLike')
->where('privacy', 'EVERYONE')
->orWhereIn('user', $friends)
->whereNotIn('user', $blockedUserArray)
->latest()
->paginate($this->videoPaginate);
我可以通过两个查询来做到这一点,但是它将影响分页。 有没有办法在单个查询中做到这一点?
您必须将这两个查询分组
->where('privacy', 'EVERYONE')
->orWhereIn('user', $friends)
至
->where($query, function($query) use ($friends) {
$query->where('privacy', 'EVERYONE')
->orWhereIn('user', $friends);
})
检查文档参数分组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.