0
let me just give you guys the case of what problem i've found :
i wanted to export my table rekan that had many to many relationship with penyakits, for example 1 rekan instances could have a lot of penyakits. what i wanted to have is that all multiple values that i got from many to many relationship to be set in 1 cell only (depending on the row of the instances that has that value).
example with table that i wanted to make in excel :
| Rekan Name | ------------Penyakits----------------- |
| -------------- | ---------------------------------------- |
| Rekan 1------| Penyakits x, penyakits y, penyakits z. |
| Rekan 2------| Penyakits x, penyakits y.-------------- |
this is my code that currently i've been working on in laravel excel using only WithStyle (cause i found that this method is the most flexible way to custom my sheets)
$rowData = 4;
foreach($rekans as $rekan){
$sheet->setCellValue('A'.$rowData,$rekan->rekan_inv);
$sheet->setCellValue('B'.$rowData,$rekan->dokter->nama_dokter);
$sheet->setCellValue('C'.$rowData,$rekan->pasien->nama);
$sheet->setCellValue('D'.$rowData,$rekan->pasien->klien->nama);
$sheet->setCellValue('E'.$rowData,$rekan->berat);
$sheet->setCellValue('F'.$rowData,$rekan->suhu);
$sheet->setCellValue('G'.$rowData,$rekan->anamnesa);
$sheet->setCellValue('H'.$rowData,$rekan->hasil_pemeriksaan);
$sheet->setCellValue('J'.$rowData,$rekan->treatment->nama_treatment);
$sheet->setCellValue('K'.$rowData,$rekan->pengobatan);
$sheet->setCellValue('L'.$rowData,$rekan->kasus);
$sheet->setCellValue('M'.$rowData,Date::dateTimeToExcel($rekan->created_at));
foreach($rekan->penyakit as $penyakit){
$sheet->setCellValue('I'.$rowData,$penyakit->nama_penyakit);
}
Answer
based on mikens idea, i manage to found the solution by using this code. thanks miken
$listpenyakit = [];
$fixpenyakit = "";
foreach($rekan->penyakit as $penyakit){
array_push($listpenyakit,$penyakit->nama_penyakit);
}
if (count($listpenyakit) > 1) {
$fixpenyakit = implode(", ",$listpenyakit);
} else {
$fixpenyakit = implode($listpenyakit);
}
$fixpenyakit .= ".";
// dd($fixpenyakit);
$sheet->setCellValue('i'.$rowData,$fixpenyakit);
0 comments:
Post a Comment
Thanks