whenLoaded('codes', function () { return $this->codes->groupBy(fn (QrCode $c) => (string) $c->status) ->map(fn ($group) => $group->count()) ->all(); }); return [ 'batch_number' => $this->batch_number, 'quantity' => $this->quantity, 'purpose' => $this->purpose, 'target_area' => $this->whenLoaded('targetArea', fn () => $this->targetArea?->code), 'target_store_id' => $this->target_store_id, 'created_by' => $this->whenLoaded('createdBy', fn () => $this->createdBy?->full_name), 'printed_at' => $this->printed_at?->toIso8601String(), 'expires_at' => $this->expires_at?->toIso8601String(), 'notes' => $this->notes, 'status_counts' => $statusCounts, 'created_at' => $this->created_at?->toIso8601String(), ]; } }