i made import csv file to laravel. this is the contents of the CSV data that I will import : data csv
when I try to import a CSV file whose contents are in the image I uploaded, which is empty/null, and I have added (Double) $row[24] ?? NULL to handle if the value is NULL then directly insert 0.0 in the table.CSV file
data type 'lemhanor' and 'lemakpek' --> data type lemhanor and lemakpek
whereas for 'lemhanor' and 'lemakpek' they have the same data type and in the CSV the values are both empty. What to do?
this is migration for absensi_table:
public function up()
{
Schema::create('absensi', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('id_karyawan');
$table->string('nik')->nullable()->default(null);
$table->date('tanggal');
$table->string('shift');
$table->time('jadwal_masuk');
$table->time('jadwal_pulang');
$table->time('jam_masuk');
$table->time('jam_keluar')->nullable()->default(null);
$table->double('normal',2,1);
$table->double('riil',2,1);
$table->time('terlambat')->nullable()->default(null);
$table->time('plg_cepat')->nullable()->default(null);
$table->string('absent',5);
$table->time('lembur')->nullable()->default(null);;
$table->time('jml_jamkerja')->nullable()->default(null);
$table->string('pengecualian')->nullable()->default(null);
$table->string('hci',5)->default(True);
$table->string('hco',5)->default(True);
$table->string('id_departemen');
$table->double('h_normal',2,1);
$table->double('ap',2,1);
$table->double('hl',2,1);
$table->time('jam_kerja')->nullable()->default(null);
$table->double('lemhanor',2,1);
$table->double('lemakpek',2,1);
$table->double('lemhali',2,1);
$table->foreign('id_karyawan')->references('id')->on('karyawan')->onDelete('cascade');
$table->timestamps();
});
}
this is AttendanceImport.php in HTTP folder:
<?php
namespace App\Imports;
use Carbon\Carbon;
use App\Models\Absensi;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
class AttendanceImport implements ToModel
{
public function startRow(): int
{
return 2;
}
public function getCsvSettings(): array
{
return [
'delimiter' => ';'
];
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
if(isset($row[0]) && isset($row[2]))
{
if(!Absensi::where('id_karyawan',$row[0])->where('tanggal',Carbon::parse($row[2])->format("Y-m-d"))->exists())
{
return new Absensi([
'id_karyawan' => $row[0],
'nik' => $row[1] ?? null,
'tanggal' => Carbon::parse($row[2])->format("Y-m-d"),
'shift' => $row[3] ?? null,
'jadwal_masuk' => $row[4] ?? null,
'jadwal_pulang' => $row[5] ?? null,
'jam_masuk' => $row[6] ?? null,
'jam_keluar' => $row[7] ?? null,
'normal' => $row[8] ?? null,
'riil' => (Double) $row[9] ?? null,
'terlambat' => $row[10] ?? null,
'plg_cepat' => $row[11] ?? null,
'absent' => $row[12] ?? null,
'lembur' => $row[13] ?? null,
'jml_jamkerja' => $row[14] ?? null,
'pengecualian' => $row[15] ?? null,
'hci' => $row[16] ?? null,
'hco' => $row[17] ?? null,
'id_departemen' => $row[18] ?? null,
'h_normal' => (Double) $row[19] ?? null,
'ap' => (Double) $row[20] ?? null,
'hl' => (Double) $row[21] ?? null,
'jam_kerja' => $row[22] ?? null,
'lemhanor' => (Double) $row[23] ?? null,
'lemakpek' => (Double) $row[24] ?? null,
'lemhali' => (Double) $row[25] ?? null,
]);
// dd($row);
}
// else{
// Log::info('id karyawan dan tanggal absensi sudah ada');
// }
}
// else{
// Log::info('Row 0 dan 2 kosong');
// }
}
}
Controller:
public function importcsv(Request $request)
{
Excel::import(new AttendanceImport, request()->file('file'));
return redirect()->back()->with('success','Data Imported Successfully');
}