Home > Mobile >  laravel excel import update only if there is a change
laravel excel import update only if there is a change

Time:08-08

i have laravel excel maatwebsite import update function, it works really well but i want to mark record that change by add "correction_flag" variable. the question is, how can i set this "correction_flag" when import.

this is my import function:

public function collection(Collection $rows)
{   
          
    foreach ($rows as $row) 
    {   
        $tgl_lahir_cell = $this->transformDate($row['tanggal_lahir']);
        $tgl_awal_masa_kerja_cell = $this->transformDate($row['tanggal_awal_masa_kerja']);
        $tgl_menjadi_permanen_cell = $this->transformDate($row['tanggal_menjadi_permanen']);
        $tgl_keluar_cell = $this->transformDate($row['tanggal_keluar']);

        if($tgl_lahir_cell == '1970-01-01') {
            $formatedDate1 = NULL;
        }else{
            $formatedDate1 = $tgl_lahir_cell;
        }

        if($tgl_awal_masa_kerja_cell == '1970-01-01') {
            $formatedDate2 = NULL;
        }else{
            $formatedDate2 = $tgl_awal_masa_kerja_cell;
        }
        
        if($tgl_menjadi_permanen_cell == '1970-01-01') {
            $formatedDate3 = NULL;
        }else{
            $formatedDate3 = $tgl_menjadi_permanen_cell;
        }
        
        if($tgl_keluar_cell == '1970-01-01') {
            $formatedDate4 = NULL;
        }else{
            $formatedDate4 = $tgl_keluar_cell;
        }
        
        Tempdat::where('cc', Auth::user()->ccode)
            ->where('id_tempdat', $row['id_tempdat'])
            ->update([
                'golongan' => $row['golongan'],
                'nama' => $row['nama'],
                'jenis_kelamin' => $row['jenis_kelamin'],
                'tgl_lahir' => $formatedDate1,
                'tgl_awal_masa_kerja' => $formatedDate2,
                'tgl_menjadi_permanen' => $formatedDate3,
                'status_awal' => $row['status_di_awal_periode'],
                'gaji_pokok_awal' => $row['gaji_pokok_di_awal_periode'],
                'tunjangan_tetap_awal' => $row['tunjangan_tetap_di_awal_periode'],
                'total_upah_awal' => $row['total_upah_di_awal_periode'],
                'status_akhir' => $row['status_di_akhir_periode'],
                'tgl_keluar' => $formatedDate4,
                'status2_akhir' => $row['keterangan_keluar'],
                'gaji_pokok_akhir' => $row['gaji_pokok_di_akhir_periode'],
                'tunjangan_tetap_akhir' => $row['tunjangan_tetap_di_akhir_periode'],
                'total_upah_akhir' => $row['total_upah_di_akhir_periode'],
                'jumlah_pesangon_paid_awal' => $row['pesangon_dibayarkan_pada_periode'],
                'correction_flag' => 'CORRECTED'
            ]);
    }
}

public function headingRow(): int
{
    return 4;
}

public function startRow(): int
{
    return 5;
}

i already tried to put correction_flag like the example above but its just make all record uploaded marked CORRECTED eventhou there is no changes happend.

CodePudding user response:

You need to first() the query, because laravel model event listeners won't work if there's mass update/create. So what you need to do is:

$tempdat = Tempdat::where('cc', Auth::user()->ccode)->where('id_tempdat', $row['id_tempdat'])->first();

if($tempdat){
    $update->update([
        ...
    ]);
}

And add this function to your Temptdat model:

protected static function booted()
    {
        parent::boot();

        self::saving(function ($model) {
            if ($model->isDirty()) {
                $model->correction_flag = 'CORRECTED';
            }
        });
    }
  • Related