在laravel原始查询中合并数据后返回状态
我有一个查询,选择一个日期,然后将生成数据和现有销售存款的总和。total有两个状态/is\u verified 0和1 例如,我选择的日期为2020-01-01到2020-01-31,如果现有销售存款。total的状态为0,它将返回一个待定, 如果销售库存存款。总计所有状态为1,则将返回已批准的在laravel原始查询中合并数据后返回状态,laravel,Laravel,我有一个查询,选择一个日期,然后将生成数据和现有销售存款的总和。total有两个状态/is\u verified 0和1 例如,我选择的日期为2020-01-01到2020-01-31,如果现有销售存款。total的状态为0,它将返回一个待定, 如果销售库存存款。总计所有状态为1,则将返回已批准的 $result = $this->sales_on_hand->where(function($query) use($request, $dateStart, $dateStart2){
$result = $this->sales_on_hand->where(function($query) use($request, $dateStart, $dateStart2){
$query->whereBetween('sales_onhands.created_at', [$dateStart, $dateStart2]);
if($request->has('branch') && $request->branch != ""){
$query->where('branch_id', $request->branch);
}
})->join('sales_onhand_deposits','sales_onhands.id','=','sales_onhand_deposits.sales_onhand_id'
)->select(
DB::raw(
'
IFNULL(SUM(sales_onhands.cash_on_hand), 0) AS cash_onhands,
IFNULL(SUM(sales_onhands.gc_onhand), 0) AS gc_onhands,
IFNULL(SUM(sales_onhand_deposits.total), 0) AS total_deposit,
IFNULL(SUM(case when sales_onhand_deposits.is_verified = 0 then sales_onhand_deposits.total else 0 end), 0) AS total_pending_deposit,
IFNULL(SUM(case when sales_onhand_deposits.is_verified = 1 then sales_onhand_deposits.total else 0 end), 0) AS total_approve_deposit
'
)
)->get();
我猜您可以检查所有记录的计数,其中“已验证记录的条件计数”为1,如果这两个计数匹配,则表示所有记录的计数均为“已验证记录的条件计数”为1,最终结果将被视为已批准
DB::raw(
' ....,
IF(COUNT(*) = SUM(case when sales_onhand_deposits.is_verified = 1 then 1 else 0 end), "approved", "pending") AS sale_status
'
)