getExportClass($employeesToExport); return Excel::store($exportClass, $filename, $diskName); } /** * @param \App\Models\Employee[] $employeesToExport * @return FromCollection&WithCustomCsvSettings&WithHeadings&WithMapping<\App\Models\Employee> */ private function getExportClass(array $employeesToExport): FromCollection & WithCustomCsvSettings & WithHeadings & WithMapping { return new readonly class ($employeesToExport) implements FromCollection, WithCustomCsvSettings, WithHeadings, WithMapping { /** @param \App\Models\Employee[] $employeesToExport */ public function __construct( private array $employeesToExport ) { } /** @return string[] */ public function headings(): array { return [ 'Matricule', 'Nom', 'Prenom', 'Site', 'Code', ]; } /** @param \App\Models\Employee $employee */ public function map($employee): array { /** @var \App\Models\Employee $employee */ return [ $employee->matricule->value, $employee->nom, $employee->prenom, $employee->site, $employee->code, ]; } /** @return array */ public function getCsvSettings(): array { return [ 'delimiter' => ';', 'output_encoding' => 'UTF-8', ]; } /** @return \Illuminate\Support\Enumerable */ public function collection(): Enumerable { return Collection::make($this->employeesToExport); } }; } }