Laravel检查查询次数值

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

在我的应用程序中,我有以下查询。

public function ExcOrders($driver_id, $from_date, $to_date){

        $orders = DB::table('order_details as o')
                    ->join('order_tracker as a','o.id','=','a.oid')
                    ->where('a.owner_id','=',$driver_id)
                    ->whereBetween('a.created_at',[$from_date, $to_date]) 
                    ->where('o.status',0)
                    ->select([DB::raw('SUM(o.price) as TotalDeposits'),DB::raw('count(o.price) as count')])
                    ->get();

        if($orders->count() > 0){
            return response()->json(['status_code'=>1000,'data'=>$orders , 'message'=>"null"],200);
        }else{
            return response()->json(['status_code'=>2000,'data'=>null , 'message'=>"You Have No Orders For Now !"],200);
        }
    }

它的工作正常,并返回所需的结果,但我的问题,如返回所示,我需要检查是否计数值大于零,我不能。

结果总是像下面这样。

{
    "status_code": 1000,
    "data": [
        {
            "TotalDeposits": null,
            "count": 0
        }
    ],
    "message": "null"
}

衷心感谢

mysql laravel lumen
1个回答
1
投票
public function ExcOrders($driver_id, $from_date, $to_date)
{
    $orders = DB::table('order_details as o')
        ->join('order_tracker as a', 'o.id', '=', 'a.oid')
        ->where('a.owner_id', '=', $driver_id)
        ->whereBetween('a.created_at', [$from_date, $to_date])
        ->where('o.status', 0)
        ->select([DB::raw('SUM(o.price) as TotalDeposits'), DB::raw('count(o.price) as count')])
        ->first(); //replaced get here 

    if ($orders->count > 0) { // check here - if you need you may check $orders first too 
        return response()->json(['status_code' => 1000, 'data' => $orders, 'message' => "null"], 200);
    }

    return response()->json(['status_code' => 2000, 'data' => null, 'message' => "You Have No Orders For Now !"], 200);
}

0
投票

试试这个。

$orders = $orders->reject(function ($order) {
    // do somthing ...
    return $order->data->count >= 'some condition you like';
})
->map(function ($order) {
    // do somthing ...
    return $order;
});
© www.soinside.com 2019 - 2024. All rights reserved.