1 回答

TA貢獻1828條經(jīng)驗 獲得超3個贊
由于您的$user未正確初始化。確保您獲得要更新的記錄。
并在值初始化后始終寫下您在代碼中使用的變量并正確條件。
還要始終使用 try catch 語句來快速調(diào)試您的問題。可能下面的代碼將對您有所幫助。
try {
$validated = request()->validate([
'userId' => 'required',
'newFirstName' => 'required|string|max:255',
'newLastName' => 'required|string|max:255',
'newEmail' => 'required|string|email|max:255',
'newPhone' => 'nullable'
]);
if ($validated->fails()) {
return response($validated);
}else{
if (!blank($user)) {
$user = User::find($validated['userId']);
$user->first_name = $validated['newFirstName'];
$user->last_name = $validated['newLastName'];
$user->email = $validated['newEmail'];
$user->phones = $validated['newPhone'];
$user->name = $validated['newLastName'] . ' ' . $validated['newFirstName'];
$user->save();
}else{
//throw error response.
}
}
} catch (\Exception $ex) {
return response(ExceptionMessage($ex));
}
- 1 回答
- 0 關(guān)注
- 100 瀏覽
添加回答
舉報