Hiệu chỉnh api update technical

This commit is contained in:
Truong Vo 2024-09-24 16:18:03 +07:00
parent 40711691ab
commit c0826f814e
1 changed files with 21 additions and 8 deletions

View File

@ -221,16 +221,29 @@ class TechnicalController extends Controller
'technicals.*.point' => 'required|integer|min:0|max:3', // Điểm trong khoảng [0-3] 'technicals.*.point' => 'required|integer|min:0|max:3', // Điểm trong khoảng [0-3]
]); ]);
// Xóa hết các bản ghi hiện có của user trong bảng technical_users // Lấy danh sách technicals của user từ bảng TechnicalUser
TechnicalUser::where('user_id', $userInfo->id)->delete(); $existingTechnicals = TechnicalUser::where('user_id', $userInfo->id)->get()->keyBy('technical_id');
// Duyệt qua mảng technicals và thêm mới dữ liệu
foreach ($validatedData['technicals'] as $technical) { foreach ($validatedData['technicals'] as $technical) {
TechnicalUser::create([ $existingTechnical = $existingTechnicals->get($technical['technical_id']);
'user_id' => $userInfo->id, // if ($technical['technical_id'] == 7) {
'technical_id' => $technical['technical_id'], // dd($existingTechnical,$userInfo);
'point' => $technical['point'] // }
]); if ($existingTechnical) {
// Nếu technical_id đã tồn tại và point khác, thì update
if ($existingTechnical->point !== $technical['point']) {
$existingTechnical->update([
'point' => $technical['point']
]);
}
} else {
// Nếu technical_id chưa tồn tại, tạo mới
TechnicalUser::create([
'user_id' => $userInfo->id,
'technical_id' => $technical['technical_id'],
'point' => $technical['point']
]);
}
} }
return AbstractController::ResultSuccess('Technicals for user updated successfully.'); return AbstractController::ResultSuccess('Technicals for user updated successfully.');