这是我的路线:
// Edit User Infos
Route::put('/edit/subscriber', 'Admin\UserController@editSubscriber')->name('/edit/subscriber');
公小号呼叫:
axios.put('/admin/edit/subscriber', {
userId: this.userDetails._id,
newFirstName: this.tempValueFirstName,
newLastName: this.tempValueLastName,
newPhone: this.tempValuePhone,
newEmail: this.tempValueEmail
}).then(({res}) => {
console.log(res)
}).catch(({err}) => {
console.log(err)
});
控制器中的更新功能:
public function editSubscriber() {
$validated = request()->validate([
'userId' => 'required',
'newFirstName' => 'required|string|max:255',
'newLastName' => 'required|string|max:255',
'newEmail' => 'required|string|email|max:255',
'newPhone' => 'nullable'
]);
$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();
}
当我尝试使用我的更新功能时,更改像我想要的那样更新,但我得到这个500错误:“从空值创建默认对象”。
有人有想法吗?谢谢。
子衿沉夜