【问题标题】:Laravel check query count valueLaravel 检查查询计数值
【发布时间】:2020-09-03 04:43:05
【问题描述】:

我的应用中有以下查询,

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"
}

任何帮助将不胜感激

【问题讨论】:

  • 您使用的是->get();,这意味着可能有多个结果。如果是多个,您将如何确定count > 0 - 是否至少其中之一?所有这些?
  • @Ersoy 结果只有 TotalDeposits 和计数,我需要检查计数值
  • 所以你返回一个结果,然后不需要使用 get - 我将发布作为答案

标签: mysql laravel lumen


【解决方案1】:

试试这个:

$orders = $orders->reject(function ($order) {
    // do somthing ...
    return $order->data->count >= 'some condition you like';
})
->map(function ($order) {
    // do somthing ...
    return $order;
});

【讨论】:

    【解决方案2】:
    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);
    }
    

    【讨论】:

    • 谢谢,但我没有得到 1 个原始结果!,我需要查询来获取与 raws 匹配的所有条件的总和和计数,请检查我的输出如何获得计数值,仅此而已我需要
    • @AliAdil 如果您的响应是数组,但其中只有一个元素(每次),那么您不需要将其作为数组获取。如果它是数组,但有多个对象由 {totalDeposits 和 count} 组成,那么您将如何决定 the count > 0。由于其中一些可能为零,因此其中一些可能大于零。
    • 只是为了让所有事情都清楚,但据我所知, first() 函数将只选择与查询匹配的第一个原始数据?!就我而言,我有几个原料
    • 好的,所以我不能在我的情况下使用它,因为我提到我有几个 raws,因此我必须使用 get()
    • 假设这是[{"TotalDeposits": null,"count": 0}, {"TotalDeposits": 2,"count": 2}, {"TotalDeposits": 1,"count": 1}] 的响应,有两行 > 0 和 1 行是 0 - 你想如何进行比较,会做出什么决定? at least one row with the value > 0 all the rows > 0 或其他 @AliAdil
    猜你喜欢
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 2017-11-05
    • 2016-11-19
    • 2021-11-21
    • 1970-01-01
    相关资源
    最近更新 更多