resultBatches() ->with(['items' => function ($q): void { $q->orderBy('prize_type')->orderBy('prize_index'); }]) ->orderByDesc('result_version') ->get(); return ApiResponse::success([ 'draw_id' => (int) $draw->id, 'draw_no' => $draw->draw_no, 'draw_status' => $draw->status, 'batches' => $batches->map(fn (DrawResultBatch $b) => $this->serializeBatch($b))->all(), ]); } /** @return array */ private function serializeBatch(DrawResultBatch $batch): array { return [ 'id' => (int) $batch->id, 'result_version' => (int) $batch->result_version, 'source_type' => $batch->source_type, 'rng_seed_hash' => $batch->rng_seed_hash, 'status' => $batch->status, 'created_by' => $batch->created_by, 'confirmed_by' => $batch->confirmed_by, 'confirmed_at' => $batch->confirmed_at?->toIso8601String(), 'created_at' => $batch->created_at?->toIso8601String(), 'updated_at' => $batch->updated_at?->toIso8601String(), 'items' => $batch->items->map(fn (DrawResultItem $item) => [ 'prize_type' => $item->prize_type, 'prize_index' => (int) $item->prize_index, 'number_4d' => $item->number_4d, 'suffix_3d' => $item->suffix_3d, 'suffix_2d' => $item->suffix_2d, 'head_digit' => $item->head_digit, 'tail_digit' => $item->tail_digit, ])->values()->all(), ]; } }