laravel 各时间段数量统计

admin 1个月前 37 阅读
use Carbon\\Carbon;
 
public function getNumber()
{
        $data = [];
 
        #今天数据
        $data['customer_today'] = Customer::where('customer_type', 1)->where('created_at', Carbon::today())->count();
        #昨天数据
        $data['customer_yesterday'] = Customer::where('customer_type', 1)->where('created_at', Carbon::yesterday())->count();
 
        // 本周数据
        $this_week = [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()];
        $data['customer_this_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $this_week)->count();
 
        // 上周数据
        $last_week = [Carbon::now()->startOfWeek()->subWeek(), Carbon::now()->endOfWeek()->subWeek()];
        $data['customer_last_week'] = Customer::where('customer_type', 1)->whereBetween('created_at', $last_week)->count();
 
        // 本月数据
        $data['customer_this_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->month)->count();
 
        // 上月数据
        $data['customer_last_month'] = Customer::where('customer_type', 1)->whereMonth('created_at', Carbon::now()->subMonth()->month)->count();
 
        // 本年数据
        $data['customer_this_year'] = Customer::where('customer_type', 1)->whereYear('created_at', Carbon::now()->year)->count();
 
       
        return $data;
}
回复 0
登录之后才能参与