简体   繁体   English

LARAVEL 9:如何使用 maatwebsite 根据过滤数据(id、月和年)将数据导出到 excel

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

I'm making an export data to excel based on employee_id filter, month and year of absence:我正在根据 employee_id 过滤器、缺勤月份和年份将数据导出到 excel: 过滤数据 . .

when the filter is submitted the data appears in the table below:提交过滤器后,数据显示在下表中: 数据

I have managed to get the data, but when the Download Excel button is clicked, the contents are just empty Excel, like this:我设法获取了数据,但是当单击“下载 Excel”按钮时,内容只是空 Excel,如下所示: 擅长

the data does not enter the excel.数据没有进入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:我的 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请帮忙

Currently, you get no response to this statement because $this->id_karyawan is null as you have not properly passed the value through.目前,您没有收到对此语句的响应,因为$this->id_karyawannull ,因为您没有正确传递该值。

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

Above, you are passing an array of values to the Export class.在上面,您将一组值传递给 Export 类。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM