其他分享
首页 > 其他分享> > laravel carbon 统计七天数据

laravel carbon 统计七天数据

作者:互联网

        $where = ['recycle_user_id' => Auth::user()->user_id];

        $today = [
            Carbon::now()->startOfDay(),
            Carbon::now()->endOfDay()
        ];
        $seven = [
            Carbon::now()->subDays(7),
            Carbon::now()->startOfDay(),
        ];
        $month = [
            Carbon::now()->subDays(30),
            Carbon::now()->startOfDay(),
        ];

        $data = \DB::table('order')->select([
            \DB::raw('sum(case when status = "create" then 1 else 0 end) as create_count '),
            \DB::raw('sum(case when status = "finish" then 1 else 0 end) as finish_count '),
            \DB::raw('sum(case when status = "buyer_cancle" or status = "close" then 1 else 0 end) as cancle_count '),
            \DB::raw('sum(case when status = "create" or status = "finish" then 1 else 0 end) as all_count '),
        ])->where($where)->get()->toArray();

        $data = (array)$data[0];

        foreach (compact('today', 'seven', 'month') as $name => $between) {
            $data[$name] = \App\Models\Order::query()->where($where)->whereNotIn('status', \App\Models\Order::$paymentStatus)->whereBetween('created_at', $between)->sum('amount');
        }

        return $data;

标签:laravel,status,七天,sum,DB,Carbon,carbon,now,data
来源: https://www.cnblogs.com/liaosp/p/laravel_seven_day_carbon.html