I try to make condition where i delete data before insert data to prevent multiple data but it didnt work | even the data in table become empty || 'id_lokasi' is my column name in table - database
$factpelanggan4 = DB::connection('clickhouse')
->select("SELECT t.id_tahun, l.id_lokasi, stage.jml_pelanggan
from dim_tahun t
inner join stage_pelanggan stage on t.tahun = stage.years
inner join dim_lokasi l on stage.city_id = l.id_kota
WHERE (stage.country_id = l.id_negara)
ORDER BY t.id_tahun ASC , l.id_lokasi ASC, stage.jml_pelanggan ASC
");
foreach ($factpelanggan4 as $value) {
$tahun_id[] = $value['t.id_tahun'];
$lokasi_id[] = $value['l.id_lokasi'];
$jml[] = $value['stage.jml_pelanggan'];
}
DB::connection('clickhouse')->table('fakta_pelanggan')->where('id_lokasi', $lokasi_id)->delete();
//DB::connection('clickhouse')->table('fakta_pelanggan')->where('id_lokasi', Not Null)->delete();
foreach ($factpelanggan4 as $key => $value) {
$arrayInsert = array(
'id_tahun' => $tahun_id[$key],
'id_lokasi' => $lokasi_id[$key],
'jml_pelanggan' => $jml[$key],
);
DB::connection('clickhouse')->table('fakta_pelanggan')->insert($arrayInsert);
$key++;
}