有3张桌子
每个帖子都有一些标签,每个用户都有一些角色
所有用户都可以看到公开信息和 只有具有角色的用户才能看到在post-tags-title中具有以下角色-title的帖子
我需要从查询中删除一些记录
// hasRole mean Only visible to users who have role
$allPosts = Post::where('type', 'public')
->orWhere('type', 'hasRole')
->get();
// all user roles
$allUserRoles = Role::where('user_id', Auth::user()->id)->select('roleTitle')->get();
foreach ($allPosts as $post)
{
if($post->type == 'hasRole')
{
// just visible this post to anyOne who have these role
$allPostTags = Tag::where('post_id', $post->id)->select('tagTitle')->get();
// $subscribe between postTags and userRoles
$subscribe = array_intersect($allPostTags, $allUserRoles);
if (count($subscribe) < 1) {
// remove this record from $allPosts
// ???
}
}
}
return $allPosts->take(10);
我可以一次获得所有公开的帖子,然后将它们与具有特定访问权限的帖子合并,然后按日期排序帖子吗?
有没有更简单的方法?