我有一个视图,其中加载和显示来自数据库的帖子。每个帖子都有用户可以设置的隐私。如果隐私为1,则任何人都可以查看该帖子。如果隐私为2,则该帖子只能由订阅用户的用户查看。
我如何才能只向订阅者而不向任何人展示带有隐私的帖子?
UserModel上的辅助类
public function isFollowed()
{
return $this->followers()
->where('follower_id', auth()->id())
->where('followers.active', 1)
->exists();
}负载柱
$posts = Post::with('comments', 'tags')->whereIn('posts.privacy', [1, 2])->where('posts.status', 1)->where('posts.archived', 0)->where('posts.type', '!=', 7)->latest()->paginate(15);发布于 2019-08-17 13:46:49
您只需在约束范围内进行区分。因为您有一些其他约束,所以有必要将OR连接约束放在它们自己的where()中。
use Illuminate\Database\Eloquent\Builder;
$posts = Post::query()
->with('comments', 'tags')
->where('posts.status', 1)
->where('posts.archived', 0)
->where('posts.type', '!=', 7)
->where(function (Builder $query) {
$query>where('posts.privacy', 1)
->orWhere(function (Builder $query) {
$query->where('posts.privacy', 2)
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('followers')
->whereColumn('followers.user_id', 'posts.user_id')
->where('followers.follower_id', auth()->id())
->where('followers.active', 1);
});
});
})
->latest()
->paginate(15);我不太确定followers表上的第二列除了follower_id之外是什么,所以我假定是user_id。这很可能需要改变。
顺便说一句,你所描述的听起来更像是能见度,但不是隐私。如果我可以订阅你(没有限制和接受你的),我突然看到你更多的内容,这几乎是隐私。
https://stackoverflow.com/questions/57535886
复制相似问题