whereKey($draw->id)->lockForUpdate()->firstOrFail(); if ($locked->status !== DrawStatus::Pending->value) { throw new \RuntimeException('draw_not_deletable'); } if ($locked->resultBatches()->exists()) { throw new \RuntimeException('draw_result_exists'); } $betTotal = (int) TicketOrder::query()->where('draw_id', $locked->id)->sum('total_actual_deduct'); if ($betTotal > 0) { throw new \RuntimeException('draw_has_bets'); } $locked->delete(); }); } }