| Server IP : 35.80.110.71 / Your IP : 216.73.216.221 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ip-172-31-21-44 6.17.0-1019-aws #19~24.04.1-Ubuntu SMP Tue Jun 23 18:53:06 UTC 2026 x86_64 User : ubuntu ( 1000) PHP Version : 8.3.31 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/client-portal-laravel/releases/20260626120707/app/Exports/ |
Upload File : |
<?php
declare(strict_types=1);
namespace App\Exports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class GenericExport implements FromCollection, ShouldAutoSize, WithHeadings, WithMapping, WithStyles
{
/**
* @param array<string, string> $columns Key => Header mapping
*/
public function __construct(
private readonly Collection $data,
private readonly array $columns
) {}
public function collection(): Collection
{
return $this->data;
}
/**
* @return array<int, string>
*/
public function headings(): array
{
return array_values($this->columns);
}
/**
* @param mixed $row
* @return array<int, mixed>
*/
public function map($row): array
{
$rowData = [];
foreach (array_keys($this->columns) as $key) {
$value = data_get($row, $key, '');
// Handle arrays and objects
if (is_array($value) || is_object($value)) {
$value = json_encode($value);
}
$rowData[] = $value;
}
return $rowData;
}
public function styles(Worksheet $sheet): array
{
return [
// Style the first row as bold (headers)
1 => ['font' => ['bold' => true]],
];
}
}