首页 > 解决方案 > 从表中选择(laravel) - where 和 orWhere

问题描述

所以,我正在使用 laravel,我想让这个查询在 laravel 'formula' 中工作

SELECT * FROM `complaints` WHERE (`fID` = 0) AND (`hide_topic` = 0 OR (`hide_topic` <= 0 OR (`hide_topic` > 3 AND byID = 52))) ORDER BY `status` ASC, `id` DESC

我在 laravel 中做了类似的事情,但不确定是否像上面的查询那样正确选择。

               DB::table('complaints')
                          ->where('fID', '=', 0)
                                ->where('hide_topic', '=', 0)
                                ->orWhere('hide_topic', '<', 0)
                                ->orWhere('hide_topic', '>', 3)
                                    ->where('byID', '=', 52)
                           ->orderBy('status', 'asc')
                           ->orderBy('id', 'desc')
                           ->get();

有什么帮助吗?

标签: laravel

解决方案


您只需将where 与 clouser 一起用于每个复合条件:

   $admin_level=3;
    $playerid=52;

    DB::table('complaints')->where('fID', '=', 0)
        ->where(function ($query)use($admin_level,$playerid){
            $query->where('hide_topic', '=', 0)->orWhere(function ($query)use($admin_level,$playerid){
                $query->Where('hide_topic', '<', 0)->orWhere(function ($query)use($admin_level,$playerid){
                    $query ->Where('hide_topic', '>', $admin_level)
                        ->where('byID', '=', $playerid);
                });
            });
        })
        ->orderBy('status', 'asc')
        ->orderBy('id', 'desc')
        ->get();

推荐阅读