where('type', OrderType::FADAN) ->whereIn('lottery_state', [LottState::WIN, LottState::SEND]) ->groupBy('customer_id') ->get(); $customerIds = collect($orders)->pluck('customer_id')->toArray(); if (!$customerIds) { dd('not found customer'); return ; } $customers = Customer::whereIn('id', $customerIds)->get(); foreach ($customers as $customer) { $orderIds = Order::where('type', OrderType::FADAN) ->whereIn('lottery_state', [LottState::WIN, LottState::SEND]) ->where('customer_id', $customer->id) ->pluck('id')->toArray(); if (!$orderIds) { dump('customer:'.$customer->id. ' not found fadan'); continue; } $gendanNum = Order::whereIn('pid', $orderIds) ->whereIn('lottery_state', [LottState::WIN, LottState::SEND, LottState::LOSS]) ->count(); Customer::where('id', $customer->id) ->update([ 'gendan_num' => $gendanNum ]); } } }