user(); $month = $request->month ?? now()->format('Y-m'); $action = $request->action ?? 'search'; $month = explode('-', $month); $coupons = Coupon::where('user_id', $user->id) ->whereYear('created_at', $month[0]) ->whereMonth('created_at', $month[1]) ->where('status', 2) ->get(['id', 'thirdPartyGoodsId', 'created_at']); $coupons = $coupons->groupBy('create_day')->map(function ($items, $key) { $data = [ 'day' => $key, 'ysd10' => $items->where('thirdPartyGoodsId', 'YSD-full100-10')->count(), 'ysd25' => $items->where('thirdPartyGoodsId', 'YSD-full100-25')->count(), 'ysd50' => $items->where('thirdPartyGoodsId', 'YSD-full100-50')->count(), 'ysd100' => $items->where('thirdPartyGoodsId', 'YSD-full200-100')->count(), ]; return collect($data); }); $all = [ 'ysd10' => $coupons->sum('ysd10'), 'ysd25' => $coupons->sum('ysd25'), 'ysd50' => $coupons->sum('ysd50'), 'ysd100' => $coupons->sum('ysd100'), ]; $coupons = $coupons->sortByDesc('day'); if ($action == 'search') { return view('Merchant::census.index', compact('coupons', 'all')); } else { return (new CensusExport($month, $user))->download(); } } }