public function exportGedcom($stream): void
    {
        $buffer = FunctionsExport::reformatRecord(FunctionsExport::gedcomHeader($this, 'UTF-8'));

        $union_families = DB::table('families')
            ->where('f_file', '=', $this->id)
            ->select(['f_gedcom AS gedcom', 'f_id AS xref', DB::raw('LENGTH(f_id) AS len'), DB::raw('2 AS n')]);

        $union_sources = DB::table('sources')
            ->where('s_file', '=', $this->id)
            ->select(['s_gedcom AS gedcom', 's_id AS xref', DB::raw('LENGTH(s_id) AS len'), DB::raw('3 AS n')]);

        $union_other = DB::table('other')
            ->where('o_file', '=', $this->id)
            ->whereNotIn('o_type', ['HEAD', 'TRLR'])
            ->select(['o_gedcom AS gedcom', 'o_id AS xref', DB::raw('LENGTH(o_id) AS len'), DB::raw('4 AS n')]);

        $union_media = DB::table('media')
            ->where('m_file', '=', $this->id)
            ->select(['m_gedcom AS gedcom', 'm_id AS xref', DB::raw('LENGTH(m_id) AS len'), DB::raw('5 AS n')]);

        DB::table('individuals')
            ->where('i_file', '=', $this->id)
            ->select(['i_gedcom AS gedcom', 'i_id AS xref', DB::raw('LENGTH(i_id) AS len'), DB::raw('1 AS n')])
            ->union($union_families)
            ->union($union_sources)
            ->union($union_other)
            ->union($union_media)
            ->orderBy('n')
            ->orderBy('len')
            ->orderBy('xref')
            ->chunk(100, static function (Collection $rows) use ($stream, &$buffer): void {
                foreach ($rows as $row) {
                    $buffer .= FunctionsExport::reformatRecord($row->gedcom);
                    if (strlen($buffer) > 65535) {
                        fwrite($stream, $buffer);
                        $buffer = '';
                    }
                }
            });

        fwrite($stream, $buffer . '0 TRLR' . Gedcom::EOL);
    }