Php Laravel 5原始查询到查询生成器

Php Laravel 5原始查询到查询生成器,php,laravel-5,query-builder,Php,Laravel 5,Query Builder,我有下面的原始mysql查询,我想做的是将其更改为查询生成器。我需要帮助 SET @groupIds = '11,14'; SET @eventIds = '13,5'; SET @friendIds = '1000022'; SET @userFollowings = '1000001'; $query = "SELECT sn_posts.* FROM sn_posts WHERE sn_posts.deleted_at is null AND ( sn_posts.user

我有下面的原始mysql查询,我想做的是将其更改为查询生成器。我需要帮助

SET @groupIds = '11,14';
SET @eventIds = '13,5';
SET @friendIds = '1000022';
SET @userFollowings = '1000001';


$query = "SELECT sn_posts.*
FROM sn_posts
WHERE 
sn_posts.deleted_at is null AND 
(
    sn_posts.user_id = '1000042' OR (
        (
            IF(@groupIds is not null AND @groupIds <> '',FIND_IN_SET(sn_posts.postable_id,@groupIds),0)
            OR IF(@eventIds is not null AND @eventIds <> '',FIND_IN_SET(sn_posts.postable_id,@eventIds),0)
            OR IF(sn_posts.privacy_level = 0, (
                IF(@friendIds is not null AND @friendIds <> '', FIND_IN_SET(sn_posts.`user_id`,@friendIds),0)
                OR IF(@userFollowings is not null AND @userFollowings <> '',FIND_IN_SET(sn_posts.`user_id`,@userFollowings),0)
                ), 
                IF(@friendIds is not null AND @friendIds <> '', FIND_IN_SET(sn_posts.user_id,@friendIds),0)
            )
        )
        AND IF(sn_posts.`parent_id` IS NOT NULL,(
            SELECT parent.id FROM sn_posts AS parent
            WHERE parent.id = sn_posts.`parent_id` AND IF(parent.privacy_level = 0,1,IF(@friendIds is not null AND @friendIds <> '', FIND_IN_SET(parent.`user_id`,@friendIds),0))
            ),1)
    )
)

ORDER BY sn_posts.created_at desc
LIMIT 21,10";
$result = DB::raw(DB::select($query));
我有php变量,而不是那些mysql变量。 因为需要将结果作为集合,所以我需要一种将结果作为集合获取的方法。 我所尝试的:

$result = Post::where('deleted_at', null)
        ->where(function($query) use($authUserID,$friendIds,$userFollowings,$groupIds,$eventIds) {
            $query->where('user_id', $authUserID);
                orWhere(function($query) use($friendIds,$userFollowings,$groupIds,$eventIds) {
                    $query->whereIn('postable_id', $groupIds)
                        ->orWhereIn('postable_id', $eventIds)
                        ->orWhere(function($query) use ($friendIds, $userFollowings) {
                            if ($query->where('privacy_level', 0)) {
                                $query->whereIn('user_id', $friendIds)
                                    ->orWhereIn('user_id', $userFollowings);
                            } else {
                                $query->whereIn('user_id', $friendIds);
                            }
                        });
                })->where(function($query) use($friendIds){
                    ***if ($query->whereNotNull('parent_id')) {
                        DB::raw('select parent.id from sn_posts as parent where parent.id = '.$query->parent_id.' 
                        and if(parent.privacy_level = 0,1, 
                        if('.$friendIds.' is not null and '.$friendIds.' <> "", FIND_IN_SET(parent.user_id, '.$friendIds.'), 0))
                    }***
                });
        });

主要问题是在当前共享帖子的星形块之间检查父帖子的访问级别。这意味着我要检查当前帖子是否是共享帖子,然后检查它是否是公开帖子,或者它是否是我朋友的帖子,然后获取共享帖子,否则退出。

@Rahul我已经尝试过了,但有很多问题,我真的不知道该怎么做。至少发布你的代码。那些“如果”和“在你的集合中找到你”显然使这不能被翻译成有说服力的字面意思*。。至少,抛出DB::raw可能有用。DB::rawDB::select$query-呃,等等,什么。。?我认为应该是DB::table'sn_posts'->select$query并返回一个键控数组。。