LARAVEL 9: How to export data to excel based on filter data (id, month and years) using maatwebsite

1.6k Views Asked by At

I'm making an export data to excel based on employee_id filter, month and year of absence: filter data.

when the filter is submitted the data appears in the table below: data

I have managed to get the data, but when the Download Excel button is clicked, the contents are just empty Excel, like this: excel

the data does not enter the excel.

My Controller:

 public function rekapabsensiExcel(Request $request)
{
    $idkaryawan = $request->id_karyawan;
    $bulan      = $request->query('bulan',Carbon::now()->format('m'));
    $tahun      = $request->query('tahun',Carbon::now()->format('Y'));

    // simpan session
    $idkaryawan = $request->session()->get('idkaryawan');
    $bulan      = $request->session()->get('bulan');
    $tahun      = $request->session()->get('tahun',);

    // dd($idkaryawan,$bulan,$tahun );

    if(isset($idkaryawan) && isset($bulan) && isset($tahun))
    {
        $data = Absensi::where('id_karyawan', $idkaryawan)
        ->whereMonth('tanggal', $bulan)
        ->whereYear('tanggal',$tahun)
        ->get();
        // dd($data);
    }else{
        $data = Absensi::all();
    }
    return Excel::download(new RekapabsensiExport(['data'=>$data, 'idkaryawan'=>$idkaryawan]),'rekap_absensi_bulanan.xlsx');
}

My RekapAbsensiExport.php:

<?php

 namespace App\Exports;

 use App\Models\Absensi;
 use Maatwebsite\Excel\Concerns\FromCollection;

class RekapabsensiExport implements FromCollection
{
protected $id_karyawan;

// function __construct($id_karyawan) {
//     $this->id_karyawan = $id_karyawan;
// }
 public function headings(): array {
    return [
        "No. ID","ID Karyawan","NIK","Tanggal","Jam Kerja","Jam Masuk","Jam Pulang",
        "Scan Masuk","Scan Pulang","Normal","Riil","Terlambat","Plg Cepat","Absent",
        "Lembur","Jml Jam Kerja","pengecualian","Harus C/I","Harus C/O","Departemen",
        "Hari Normal","Akhir Pekan","Hari Libur","Jml Kehadiran","Lembur Hari Normal",
        "Lembur Akhir Pekan","Lembur Hari Libur"
    ];
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
    return Absensi::where('id_karyawan',$this->id_karyawan)->get();
}
} 

What part did I go wrong? Please help

1

There are 1 best solutions below

1
matticustard On

Currently, you get no response to this statement because $this->id_karyawan is null as you have not properly passed the value through.

return Absensi::where('id_karyawan', $this->id_karyawan)->get();

Above, you are passing an array of values to the Export class. But your commented-out constructor function is only configured to accept a single parameter. If we stick to single parameters, you could do something like this.

return Excel::download(new RekapabsensiExport($data, $idkaryawan),'rekap_absensi_bulanan.xlsx');

Then your export constructor would look like this.

protected $data;
protected $id_karyawan;

function __construct($data, $id_karyawan) {
    $this->data = $data;
    $this->id_karyawan = $id_karyawan;
}