更新用户资料 Laravel 5.6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49601244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:35:07  来源:igfitidea点击:

Update User Profile Laravel 5.6

phplaravellaravel-5

提问by someguy

Hello I have just started in Laravel and am trying to update the users profile: My route:

您好,我刚刚开始使用 Laravel,正在尝试更新用户个人资料:我的路线:

Route::patch('users/{user}/update',  ['as' => 'users.update', 'uses' => 'UserController@update']);

My View:

我的看法:

<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>

and my update function in UserController:

和我在 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();
    }

I dont get any errors yet my user profiles aren't updated.Can sb help me?

我没有收到任何错误,但我的用户配置文件没有更新。请 sb 帮助我吗?

回答by Bostjan

I think fields aren't updated because validations fails. If you check your form you have four (4) fields with name 'email'. With this it fails validator for email.

我认为字段没有更新是因为验证失败。如果您检查表单,您将有四 (4) 个名称为“电子邮件”的字段。有了这个,它就无法通过电子邮件验证器。

You can try displaying errors in blade file: https://laravel.com/docs/5.6/validation#quick-displaying-the-validation-errors

您可以尝试在刀片文件中显示错误:https: //laravel.com/docs/5.6/validation#quick-displaying-the-validation-errors

And I suggest to inject request as method parameters. Like this;

我建议将请求作为方法参数注入。像这样;

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();
}