Laravel模型 - 想要检查DB :: raw中的位置(或)或者哪里

问题描述 投票:1回答:1

这是我的模型功能,现在它正常工作,但我想检查哪里(或)或其他地方。我已经尝试过,但无法得到合适的答案

public static function getPlacementCountByStatus(){
        $countStatus = DB::table('placements')->where('status','1')->select(DB::raw('count(joborderid) as total, joborderid'))->groupBy('joborderid')->get();
    return $countStatus;
    }

我想检查一下这样的东西

->where('statusid','=','3')->orWhere('statusid','=','4')->orWhere('stageid','=','4')->orWhere('stageid','=','8');

//我想在我的$ countStatus中检查这个和我的db :: raw查询中的和/或条件

mysql laravel
1个回答
2
投票

使用where()闭包:

$countStatus = DB::table('placements')->where(function($q){
                $q->where('statusid','=','3')->orWhere('statusid','=','4')->orWhere('stageid','=','4')->orWhere('stageid','=','8');
            })
           ->select(DB::raw('count(placementid) as total,statusid'))
           ->groupBy('statusid')->get();

 return $countStatus;

From the docs有时您可能需要创建更高级的where子句,例如"where exists"子句或嵌套参数分组。

在你的情况下,你需要使用where closure,因为你正在尝试combine AND,OR操作,意味着你试图在块中的group your parameters。这里closure主要执行parenthesis在sql查询中所做的事情。

© www.soinside.com 2019 - 2024. All rights reserved.