Skip to content

Instantly share code, notes, and snippets.

@13ey3
Last active December 5, 2020 05:34
Show Gist options
  • Select an option

  • Save 13ey3/09db500da89f70d080a47446076fd056 to your computer and use it in GitHub Desktop.

Select an option

Save 13ey3/09db500da89f70d080a47446076fd056 to your computer and use it in GitHub Desktop.
public function sync()
{
$kelurahan_oracle = DB::connection('oracle')->table("REF_KELURAHAN")->get();
$kelurahan_postgre = DB::connection('pgsql')->table("s_kelurahan")->get();
$unsetdarioracle = array();
$offset = 0;
$kodesama = false;
foreach ($kelurahan_oracle as $o_kelurahan) {
foreach ($kelurahan_postgre as $p_kelurahan) {
if ($o_kelurahan->kd_kelurahan != $p_kelurahan->s_kd_kelurahan) {
} else {
$unsetdarioracle[$offset]['kodekecamatan'] = $o_kelurahan->kd_kelurahan;
$unsetdarioracle[$offset]['namakecamatan'] = $o_kelurahan->nm_kelurahan;
$kodesama = true;
continue;
}
}
++$offset;
}
foreach ($kelurahan_oracle as $key => $value) {
foreach ($unsetdarioracle as $unset) {
if ($value->kd_kelurahan == $unset['kodekecamatan']) {
unset($kelurahan_oracle[$key]);
}
}
}
// insert data dari oracle ke postgre
$kelurahan_oracle = $kelurahan_oracle->toArray();
$kecamatan_postgre = Kecamatan::get(); // ambil dati db bphtb
$kec_to_array = [];
foreach ($kecamatan_postgre as $kec) {
$kec_to_array[number_format($kec['s_kd_kecamatan'])] = $kec['s_idkecamatan']; // simpan di array data kec dari bphtb
}
$final_data = array();
$offset = 0;
foreach ($kelurahan_oracle as $value) {
$final_data[$offset]['s_kd_propinsi'] = $value->kd_propinsi;
$final_data[$offset]['s_kd_dati2'] = $value->kd_dati2;
$final_data[$offset]['s_kd_kelurahan'] = $value->kd_kelurahan;
$final_data[$offset]['s_namakelurahan'] = $value->nm_kelurahan;
$final_data[$offset]['s_kd_kecamatan'] = $value->kd_kecamatan;
// ambil id kec berdasar kode kecamatan
$final_data[$offset]['s_idkecamatan'] = $kec_to_array[number_format($value->kd_kecamatan)];
++$offset;
}
// dd($final_data);
if (count($final_data) < 1) {
session()->flash('success', 'Data postgre sudah terupdate');
} else {
foreach ($final_data as $value) {
$insert = DB::connection('pgsql')->table('s_kelurahan')->insert($final_data);
}
}
return redirect('setting-kelurahan');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment