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;

posted @ 2022-03-05 14:49  liaosp  阅读(316)  评论(0编辑  收藏  举报