Laravel 更新密码通过验证但不更新记录

Laravel update password passes validation but doesn't update record

你好我正在使用 Laravel 并且我正在使用几个包用于用户身份验证和角色 Zizaco Confide 我想首先更新用户密码他们应该输入当前密码 a新密码并确认密码,它们应该匹配。现在验证有效,但用户密码未在数据库中更新。我的代码如下:

public function updatePassword($id) {

     $rules = array(
        'now_password'          => 'required',
        'password'              => 'min:5|confirmed|different:now_password',
        'password_confirmation' => 'required_with:password|min:5'
        );
        //password update.
        $now_password       = Input::get('now_password');
        $password           = Input::get('password');
        $passwordconf       = Input::get('password_confirmation');

        $validator = Validator::make(Input::only('now_password', 'password', 'password_confirmation'), $rules);                  

      if ($validator->fails()) {    
                return Redirect::back()->withErrors($validator);
            }
        elseif (Hash::check($now_password, Auth::user()->password)) {

                    $user = User::find($id);
                    $user->password = Hash::make($password);
                    $user->save();
          return Redirect::back()->with('success', true)->with('message','User updated.');

            } else  {
                return Redirect::back()->withErrors('Password incorrect');
            }


}

为什么没有使用此代码块更新用户密码的任何想法

$user = User::find($id);
$user->password = Hash::make($password);
$user->save();

用户模型

<?php

use Zizaco\Confide\ConfideUser;
use Zizaco\Confide\ConfideUserInterface;
use Zizaco\Entrust\HasRole;

class User extends Eloquent implements ConfideUserInterface
{
    use SoftDeletingTrait;
    use ConfideUser;
    use HasRole;

    protected $softDelete = true;

    public function favourites()
    {
        return $this->hasMany('Favourite');
    }


}

检查输入的密码:

1.

 $now_password  = Input::get('now_password');
 $user = DB::table('users')->where('name', Auth::user()->name)->first();
        if(Hash::check($now_password, $user->password)){
    //Your update here
}

2.

$now_password   = Input::get('now_password');
if(Hash::check($now_password, Auth::user()->password)){
    //Your update here
}

检查它们是否匹配以及新密码是否与旧密码不同。

$rules = array(
        'now_password'          => 'required|min:8',
        'password'              => 'required|min:8|confirmed|different:now_password',
        'password_confirmation' => 'required|min:8',
    );

并编辑您的表格(或输入您的姓名):

{{ Form::label('now_password', 'Old Password') }}
{{ Form::password('now_password')}}
{{ Form::label('password', 'New Password') }}
{{ Form::password('password')}}
{{ Form::label('password_confirmation', 'Confrim New Password') }}
{{ Form::password('password_confirmation')}}

更新

好的,所以您不想只编辑密码。

编辑您的规则:

$rules = array(
'now_password'          => 'required|min:5',
'password'              => 'min:5|confirmed|different:now_password',
'password_confirmation' => 'required_with:password|min:5'
);

我认为在每种类型的更改中都应该需要当前密码。 imo 不需要其他输入,因为您不知道用户想要编辑哪个数据。

您还应该在规则中添加如下内容:

'username'  => alpha_num|unique:users,username

等..(更多见http://laravel.com/docs/4.2/validation#available-validation-rules

如果验证器通过,你应该检查用户想要更改哪些数据(哪些输入不为空)。 类似于:

if(!empty(Input::get('firstname'))){
    $user->firstname    = Input::get('firstname');
}

等...每次输入。

这样试试:

public function updatePassword($id)
{
    $user = User::findOrFail($id);

    User::$rules['now_password'] = 'required';
    // other rules here

    $validator = Validator::make($data = Input::all(), User::rules('update', $id));

    if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }

    array_forget($data, 'password_confirmation');
    array_forget($data, 'now_password');

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

    $user->update($data);

    return Redirect::back()->with('success', true)->with('message','User updated.');
}
public function change_password_post($id)
    {
        $rules = array(
                    'current'       =>  'required|string|min:8',
                    'new'       =>  'required|string|min:8',
                    'confirm'       =>  'required|same:new'
                    );

        $validator = Validator::make(Input::only('current', 'new', 'confirm'), $rules);

        if($validator->fails())
        {
            return Redirect::back()
                ->withErrors($validator);
        }
else
            {
                $users = User::where('id', '=', $id)->first();

                if (Hash::check(Input::get('current'), $users->password))
                {
                    if(Input::get('new') == Input::get('confirm'))
                    {
                        $users->password =Hash::make(Input::get('new'));
                        $users->save();

                        $msg = array('msg' => 'Password changed Successfully');

                        return Redirect::back()
                            ->withErrors($msg);
                    }
                    else
                    {
                        $msg = array('msg' => 'New password and Confirm password did not match');

                        return Redirect::back()
                                ->withErrors($msg);
                    }
                }
                else
                {
                    $msg = array('msg' => 'Current password is incorrect');

                    return Redirect::back()
                        ->withErrors($msg);
                }
        }
    }

请记住,如果您使用第一种方式,您的密码确认字段名称必须是 "password_confirmation"... 如果您在密码确认字段中使用了另一个名称,您可以使用第二种方式。

$this->validate($request, [
            'email'    => 'required|unique:user|email|max:255',
            'username' => 'required|max:20',
            'password' => 'required|min:3|confirmed',
            'password_confirmation' => 'required',
            'gender' => 'required|in:m,f',
        ]);

$this->validate($request, [
            'email'    => 'required|unique:user|email|max:255',
            'username' => 'required|max:20',
            'password' => 'required|min:3',
            'confirm_password' => 'same:password',
            'gender' => 'required|in:m,f',
        ]);