toJson method

Map<String, dynamic> toJson()

Converts model to JSON for API responses. This respects hidden, appends, and computed.

Implementation

Map<String, dynamic> toJson() {
  final data = <String, dynamic>{
    if (id != null) 'id': id,
  };

  for (final key in _rawData.keys) {
    if (!hidden.contains(key)) {
      final value = getField(key);
      data[key] = value is DateTime ? DateHelper.toResponse(value) : value;
    }
  }

  for (final key in appends) {
    data[key] = computed[key];
  }
  // ✅ Add loaded relations
  for (final entry in _loadedRelations.entries) {
    if (!hidden.contains(entry.key)) {
      final value = entry.value;
      data[entry.key] = value;
    }
  }
  return data;
}