我需要在输入中检查用户提供的密码并进行检查。如果它是正确的,我会用新密码更新密码,如果密码错误,我想在laravel 5.4中给用户一个警告。
控制器:
public function edit(Request $request){
$user = new User;
$user = $user->find(1);
$oldpassword = $request->input('oldpassword');
$newpassword = $request->input('newpassword');
//if condition
/*if (user()->id == $oldpassword){
$updatesucess = 1;
}else {
$updatesucess = 0;
}*/
$user->update(['password' => $newpassword]);
return back();
}
视图:
@section('content')
<div class="form">
<div class="logo-area">
<img src="images/logo.png">
</div>
<form method="POST" action="/edit">
{{ csrf_field() }}
<input type="password" name="oldpassword" placeholder="old Password" required>
<input type="password" name="newpassword" placeholder="new Password" required>
<input type="submit" value="Save changes">
</form>
答案 0 :(得分:0)
使用check
外观(documentation)
Hash
方法
在您的控制器文件中添加Hash facade
使用Illuminate \ Support \ Facades \ Hash;
添加密码检查,如果不匹配,请重定向错误
$ user = User :: find(1);
$ oldpassword = $ request-&gt; input('oldpassword');
if(Hash :: check($ oldpassword,$ user-&gt; password)){ //重定向回错误 }
使用Hash :: make
更新用户密码$ newpassword = $ request-&gt; input('newpassword');
$ user-&gt; update(['password'=&gt; Hash :: make($ newpassword)]);