I use laravel 5.6
I have a json file containing 500 thousand records. I want to create a logic to check whether the id of each record already exists or not in the database. If it doesn't already exist, then there will be a data insert process. If it already exists, there will be a data update process. Before updating the data, it will check whether last_modified_date in json file and database is the same or different. If it's different then it will update
I have made logic. I just want to make sure whether my logic is effective or not
My logic code like this :
$path = storage_path('data.json');
$json = json_decode(file_get_contents($path), true);
foreach ($json['value'] as $value) {
$last_modified_date = \Carbon\Carbon::parse($value['Last_Modified_Date']);
$data = \DB::table('details')->where('id', '=', $value['Code'])->get();
if ($data->isEmpty()) {
\DB::table('details')->insert(
[
'id' => $value['Code'],
'number' => $value['Number'],
'last_modified_at' => $last_modified_date,
...
]
);
}
else {
\DB::table('details')
->where('id', '=', $value['Code'])
->where('last_modified_at', '<>', $last_modified_date)
->update([
'id' => $value['Code'],
'number' => $value['Number'],
'last_modified_at' => $last_modified_date,
...
]);
}
}
The code is working. But the process seems really long
Do you have another solution that is better?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire