label('Export (Excel/CSV)') ->icon(Heroicon::OutlinedArrowDownTray) ->color('gray') ->schema([ Toggle::make('consent_only')->label('Nur Gäste mit Einwilligung für Angebote'), ]) ->action(fn (array $data): StreamedResponse => $this->export($data['consent_only'])), CreateAction::make(), ]; } /** * Semicolon-separated UTF-8 with BOM so that Excel opens umlauts correctly. */ private function export(bool $consentOnly): StreamedResponse { $guests = Guest::query() ->whereNull('anonymized_at') ->when($consentOnly, fn ($query) => $query->withMarketingConsent()) ->withCount('bookings') ->orderBy('last_name') ->get(); return response()->streamDownload(function () use ($guests): void { $out = fopen('php://output', 'w'); fwrite($out, "\xEF\xBB\xBF"); fputcsv($out, ['Vorname', 'Nachname', 'E-Mail', 'Telefon', 'Straße', 'PLZ', 'Ort', 'Land', 'Markierungen', 'Buchungen', 'Einwilligung Angebote'], ';'); foreach ($guests as $guest) { fputcsv($out, [ $guest->first_name, $guest->last_name, $guest->email, $guest->phone, $guest->street, $guest->postal_code, $guest->city, $guest->country, implode(', ', $guest->tags ?? []), $guest->bookings_count, $guest->marketing_consent_at?->format('d.m.Y'), ], ';'); } fclose($out); }, 'gaeste-'.now()->format('Y-m-d').'.csv', ['Content-Type' => 'text/csv; charset=UTF-8']); } }