update profile password laravel 5

2019-08-12 01:20发布

问题:

I am working in laravel 5.1 and my update profile was working but will not encrypted and not working now. When I try to update the user table will also password_confirmation field and causes a conflict in the database. I do not understand. In the form says successfully but the database does not update any

Code

public function updatePassword() {
    $passwordData = Input::except('_token');
    $validation = Validator::make($passwordData, User::$passwordData);
    if ($validation->passes()) {
        array_forget($passwordData,'password_confirmation');
        User::where(array(
                'password' => Hash::make(Input::get('password'))
            ));
        Session::flash('password', 'Perfil editado com sucesso');
        return Redirect::to('backend/perfil/password');
    } else {
        return Redirect::to('backend/perfil/password')->withInput()->withErrors($validation);
    }   
}   

user

public static $passwordData = array(
        'password'              => 'required|confirmed',
        'password_confirmation' => 'required'
        );

回答1:

Follow this simple steps to get rid of anything

Step 1 : Get the password from the form

$PasswordData = Input::all();

Step 2 : Validate your password

Validator::extend('pwdvalidation', function($field, $value, $parameters) {
            return Hash::check($value, Auth::user()->password);
        });

Step 3 : Define the validation rule in your User Model

public static $rulespwd = array('OldPassword' => 'required|pwdvalidation',
        'NewPassword' => 'required|confirmed|alphaNum|min:5|max:10',
        'NewPassword_confirmation' => 'required',
        );

Note : You shall define your own rule according to your need

Step 4 : If the rule is passed, then update else throw error messages to your view

$validator = Validator::make($PasswordData, User::$rulespwd, $messages);
        if ($validator->passes()) {
            $user = User::find(Auth::user()->id);
            $user->password = Input::get('NewPassword');
            $user->save();
            return Redirect::to(Session::get('urlpath') . '/changepassword')->withInput()->with('Messages', 'The Password Information was Updated');
        } else {
            return Redirect::to(Session::get('urlpath') . '/changepassword')->withInput()->withErrors($validator);
        }