1 回答
TA贡献1828条经验 获得超3个赞
由于您的$user未正确初始化。确保您获得要更新的记录。
并在值初始化后始终写下您在代码中使用的变量并正确条件。
还要始终使用 try catch 语句来快速调试您的问题。可能下面的代码将对您有所帮助。
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 关注
- 84 浏览
添加回答
举报