first(); if (!$lottery) { dd('活动不存在'); } $tickets = TicketModel::where('lottery_id', $lottery->id)->get(); $count = $tickets->count(); if ($count < $lottery->real_count) { $tickets = $tickets->random($count); } else { $tickets = $tickets->random($lottery->real_count); } // 实际获奖名单 foreach ($tickets as $ticket) { $ticket->update(['lucky' => 1]); } // 模拟用户获奖 for ($i = $lottery->real_count; $i < $lottery->count; $i++) { TicketModel::create([ 'uid' => rand(20000001, 20010000), 'type' => '领取', 'lottery_id' => $lottery->id, 'lucky' => 1, 'ticket_code' => getChars(6), ]); } } }