Laravel 使用散列从表单更新用户密码

Laravel update user password from form with hash

所以我有这个包含 3 个字段的表单(用户电子邮件、密码和密码确认)。此表单发布到一条路线,该路线将我带到 以下控制器:

public function updateUser($id, Request $request){

    //validate post data
    $this->validate($request, [
        'email' => 'required|email',
        'password' => 'required|confirmed|min:6',
    ]);

    $request->all();

    $request['email'] = $request['email'];
    $request['password'] = Hash::make($request['password']);


    $postData =  [$request['email'], $request['password']];

    User::find($id)->update($postData);

    //store status message
    Session::flash('success_msg', 'User details updated successfully!');

    return redirect()->route('admin.user');

}

问题是,这段代码根本没有更新我的数据库,尽管它没有给我任何错误。

基本上,我想做的是允许管理员在管理面板中更改默认帐户详细信息。安装应用程序后,默认管理详细信息由播种机创建。 (此应用是单用户 - 只有管理员)

谢谢大家!

public function updateUser($id, Request $request){

    //validate post data
    $this->validate($request, [
        'email' => 'required|email',
        'password' => 'required|confirmed|min:6',
    ]);

    $input = $request->all();

    $input['email'] = $request['email'];
    $input['password'] = Hash::make($request['password']);

    User::find($id)->update($input);

    //store status message
    Session::flash('success_msg', 'User details updated successfully!');

    return redirect()->route('admin.user');

    }

您必须传递数组的键才能识别列。

public function updateUser($id, Request $request){
    //validate post data
    $this->validate($request, [
        'email' => 'required|email',
        'password' => 'required|confirmed|min:6',
    ]);
    $userData = $request->only(["email","password"]);
    $userData['password'] = Hash::make($userData['password']);
    User::find($id)->update($userData);
    Session::flash('success_msg', 'User details updated successfully!');
    return redirect()->route('admin.user');
}

您可以通过执行此操作在您的用户模型中默认设置密码散列

    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }

而不是更新用户,只需执行此操作

User::find($id)->update($request->only('email','password'));