isEmpty()) { return; } // Ambil data districts dengan normalisasi nama $districts = DB::table('districts') ->get() ->mapWithKeys(function ($item) { return [strtolower(trim($item->district_name)) => $item->district_code]; }) ->toArray(); // Cari header secara otomatis $header = $rows->first(); $headerIndex = collect($header)->search(fn($value) => !empty($value)); // Pastikan header ditemukan if ($headerIndex === false) { return; } info($rows); foreach ($rows->skip(1) as $row) { // Normalisasi nama kecamatan dan desa $districtName = strtolower(trim(str_replace('Kecamatan', '', $row[13]))); $villageName = strtolower(trim($row[14])); // Cari distric_code dari table districts $districtCode = $districts[$districtName] ?? null; $listTrueVillage = DB::table('villages') ->where('district_code', $districtCode) ->get() ->mapWithKeys(function ($item) { return [strtolower(trim($item->village_name)) => [ 'village_code' => $item->village_code, 'district_code' => $item->district_code ]]; }) ->toArray(); // ambill village code yang village_name sama dengan $villageName $villageCode = $listTrueVillage[$villageName]['village_code'] ?? '000000'; $dataToInsert[] = [ 'project_id' => $row[1], 'jenis_proyek' => $row[2], 'nib' => $row[3], 'business_name' => $row[4], 'terbit_oss' => DateTime::createFromFormat('d/m/Y', $row[5])->format('Y-m-d'), 'status_penanaman_modal' => $row[6], 'business_form' => $row[7], 'uraian_resiko_proyek' => $row[8], 'project_name' => $row[9], 'business_type_id' => $row[10], 'business_scale_id' => (int) $row[11], 'business_address' => $row[12], 'district_code' => $districtCode, 'village_code' => $villageCode, 'land_area' => $row[15], 'investment_amount' => (string) $row[16], 'number_of_employee' => (string) $row[17], ]; } if(!empty($dataToInsert)) { Tourism::insert($dataToInsert); } else { return; } } }