您好我刚刚开始使用Laravel并尝试更新用户个人资料: 我的路线:
Route::patch('users/{user}/update', ['as' => 'users.update', 'uses' => 'UserController@update']);
我的观点:
<form method="post" action="{{route('users.update', $user)}}">
{{ csrf_field() }}
{{ method_field('patch') }}
<input type="text" name="name" value="{{ $user->name }}" />
<input type="email" name="email" value="{{ $user->email }}" />
<input type="password" name="password" />
<input type="password" name="password_confirmation" />
<button type="submit">Send</button>
</form>
和我在UserController中的更新功能:
public function update(User $user)
{
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email|unique:users',
]);
$user->name = Request::input('name');
$user->email = Request::input('email');
$user->save();
Flash::message('Your account has been updated!');
return back();
}
我没有收到任何错误但我的用户个人资料没有更新。可以帮帮我吗?
答案 0 :(得分:1)
我认为字段未更新,因为验证失败。如果您检查表单,则会有四(4)个字段,其中包含姓名&#39; email&#39;。有了这个,它就失败了验证器的电子邮件。
您可以尝试在刀片文件中显示错误:https://laravel.com/docs/5.6/validation#quick-displaying-the-validation-errors
我建议将请求作为方法参数注入。像这样;
public function update(User $user, Request $request)
{
$data = $request->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
]);
$user->fill($data);
$user->save();
Flash::message('Your account has been updated!');
return back();
}