Skip to content

Instantly share code, notes, and snippets.

@faizalanwar
Created April 23, 2023 00:12
Show Gist options
  • Save faizalanwar/3d268045cd5ac6d9219e02b935191b1a to your computer and use it in GitHub Desktop.
Save faizalanwar/3d268045cd5ac6d9219e02b935191b1a to your computer and use it in GitHub Desktop.
laravel excel export with drawing
<?php
namespace App\Exports;
use App\Models\Pengaduan;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
class PengaduanExport implements FromCollection, WithHeadings, WithTitle, WithDrawings
{
protected $requestawal;
protected $requestakhir;
function __construct($requestawal, $requestakhir)
{
$this->requestawal = $requestawal;
$this->requestakhir = $requestakhir;
}
public function collection()
{
$Pengaduan = Pengaduan::select(
// 'pengaduanID',
'pengaduanTiket',
'nama_lengkap',
'no_hp',
'email',
'no_kk',
'no_ktp',
'alamat_sesuai_ktp',
'nama_pengaduan',
'jenis_pengaduan',
'isi_pengaduan',
'dokumen_pendukung',
'dokumentasi',
'status',
'keterangan',
// 'lampiran',
// 'created_by',
// 'edited_by',
// "created_at",
// "updated_at",
)->whereBetween('tanggal_surat', [$this->requestawal, $this->requestakhir])->get();
return $Pengaduan;
}
public function headings(): array
{
return [
'pengaduanTiket',
'nama_lengkap',
'no_hp',
'email',
'no_kk',
'no_ktp',
'alamat_sesuai_ktp',
'nama_pengaduan',
'jenis_pengaduan',
'isi_pengaduan',
'dokumen_pendukung',
'dokumentasi',
'status',
'keterangan',
'lampiran',
// "created_at",
// "updated_at"
];
}
public function title(): string
{
return 'Pengaduan';
}
public function drawings()
{
$Pengaduan = Pengaduan::select(
'pengaduanTiket', // a
'nama_lengkap', // b
'no_hp', // c
'email', // d
'no_kk', // e
'no_ktp', // f
'alamat_sesuai_ktp', // g
'nama_pengaduan', // h
'jenis_pengaduan', // i
'isi_pengaduan', // j
'dokumen_pendukung', // k
'dokumentasi', // l
'status', // m
'keterangan', // n
'lampiran', // o
)->whereBetween('tanggal_surat', [$this->requestawal, $this->requestakhir])->get();
$gambar1 = [];
$rowNum = 2;
foreach ($Pengaduan as $row) {
$file1 = new Drawing();
if ($row->dokumen_pendukung == null || $row->dokumen_pendukung == "") {
$file1->setPath(public_path('/assets/images/default.jpg'));
} else {
$file1->setPath(public_path('/assets/uploads/images/pengaduan/') . $row->dokumen_pendukung);
}
$file1->setheight(20);
$file1->setCoordinates('N' . $rowNum); //kolom name
$gambar1[] = $file1;
$rowNum++;
}
// return $gambar1;
$gambar2 = [];
$rowNum = 2;
foreach ($Pengaduan as $row) {
$file2 = new Drawing();
if ($row->dokumentasi == null || $row->dokumentasi == "") {
$file2->setPath(public_path('/assets/images/default.jpg'));
} else {
$file2->setPath(public_path('/assets/uploads/images/pengaduan/') . $row->dokumentasi);
}
$file2->setheight(20);
$file2->setCoordinates('N' . $rowNum); //kolom name
$gambar2[] = $file2;
$rowNum++;
}
$gambar3 = [];
$rowNum = 3;
foreach ($Pengaduan as $row) {
$file3 = new Drawing();
if ($row->lampiran == null || $row->lampiran == "") {
$file3->setPath(public_path('/assets/images/default.jpg'));
} else {
$file3->setPath(public_path('/assets/uploads/images/pengaduan/') . $row->lampiran);
}
$file3->setheight(20);
$file3->setCoordinates('N' . $rowNum); //kolom name
$gambar3[] = $file3;
$rowNum++;
}
return [$gambar1, $gambar2];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment