Laravel更新密码通过验证但不更新记录 [英] Laravel update password passes validation but doesn't update record
问题描述
public function updatePassword($ id){
$ rules = array
'now_password'=>'required',
'password'=>'min:5 |已确认|不同:now_password',
'password_confirmation'=>'required_with:password | min:5'
);
//密码更新。
$ 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-> failed()){
return Redirect :: back() - > withErrors($ validator);
}
elseif(Hash :: check($ now_password,Auth :: user() - > password)){
$ user = User :: find($ id );
$ user-> password = Hash :: make($ password);
$ user-> save();
返回重定向:: back() - >与('success',true) - >与('message','用户更新');
} else {
return Redirect :: back() - > withErrors('Password incorrect');
}
}
任何想法为什么用户密码不是不使用此代码块更新
$ user = User :: find($ id);
$ user-> password = Hash :: make($ password);
$ user-> save();
用户模型
code><?php
使用Zizaco\Confide\ConfideUser;
使用Zizaco\Confide\ConfideUserInterface;
使用Zizaco\Entrust\HasRole;
class用户扩展Eloquent实现ConfideUserInterface
{
使用SoftDeletingTrait;
使用ConfideUser;
使用HasRole;
protected $ softDelete = true;
public function favorites()
{
return $ this-> hasMany('Favorite');
}
}
要检查输入的密码:
1。
< p $ p>
$ now_password = Input :: get('now_password');
$ user = DB :: table('users') - > where('name',Auth :: user() - > name) - > first();
if(Hash :: check($ now_password,$ user-> password)){
//您的更新
}
pre> 2。
$ now_password = Input :: get('now_password');
if(Hash :: check($ now_password,Auth :: user() - > password)){
//您的更新
}
检查是否匹配,如果新密码与旧密码不同。
$ rules = array(
'now_password'=>'required | min:8',
'password'=>'required | min:8 | |不同的: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 |已确认|不同:now_password',
'password_confirmation'=>'required_with:password | min:5 '
);
我认为每种类型的更改都需要当前的密码。您不应该需要其他输入imo,因为您不知道哪个数据用户想要编辑。
您还应该添加如下规则:
'username'=> alpha_num | unique:users,username
etc ..(有关更多信息,请参阅 http://laravel.com/docs/4.2/validation#available-validation-rules )
如果验证器通过,您应该检查用户想要更改的数据(哪些输入不为空)。
如下:
if(!empty(Input :: get('firstname'))){
$ user-> firstname = Input :: get('firstname');
}
等每个输入。
Hi I am using Laravel and I am using a couple of packages for user auth and roles which are Zizaco Confide and I want to update the users password firstly they should input there current password a new password and confirm the password and they should match. Now the validation works but the users password isn't updated in the database. My code is below:
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');
}
}
Any ideas why the users password is not isn't updated using this block of code
$user = User::find($id);
$user->password = Hash::make($password);
$user->save();
User Model
<?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');
}
}
To check inputted password:
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
}
To check if they match and if the new password is different than old.
$rules = array(
'now_password' => 'required|min:8',
'password' => 'required|min:8|confirmed|different:now_password',
'password_confirmation' => 'required|min:8',
);
And edit your form to (or enter your names):
{{ 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')}}
Update
Ok, so you don't want to edit only passwords.
Edit your rules:
$rules = array(
'now_password' => 'required|min:5',
'password' => 'min:5|confirmed|different:now_password',
'password_confirmation' => 'required_with:password|min:5'
);
I think that current password should be required in every type of change. Other inputs imo shouldn't be required, because you don't know which data user want to edit.
You should also add to your rules something like:
'username' => alpha_num|unique:users,username
etc.. (for more see http://laravel.com/docs/4.2/validation#available-validation-rules)
If Validator pass, you should check which data user want to change (which inputs are not empty). Something like:
if(!empty(Input::get('firstname'))){
$user->firstname = Input::get('firstname');
}
etc...with every input.
这篇关于Laravel更新密码通过验证但不更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!