我很难调试这段代码。我只是laravel的新手,我不知道为什么即使我使用了正确的方法,这些代码也会一直重定向到同一个页面。我使用默认的laravel身份验证。我不知道我是不是错过了什么。这是代码。谢谢
<?php
namespace AppHttpControllersAuth;
use AppUser;
use AppHttpControllersController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesHash;
class ChangePasswordController extends Controller
{
public function index(){
return view('Auth.passwords.changepassword');
}
public function changepassword(Request $request){
$this->validate($request, [
'oldpassword' => 'required',
'password' => 'required|confirmed'
]);
$hashedPassword = Auth::user()->password;
if(Hash::check($request->oldpassword, $hashedPassword)) {
$user = User::find(Auth::id());
$user->password = Hash::make($request->password);
$user->save();
Auth::logout();
return redirect()->route('login')->with('successMsg', "Password successfully changed");
}else{
return redirect()->back()->with('errorMsg', "Old Password is invalid");
}
}
}
尝试此代码
$this->validate($request, [
'current_password' => 'required',
'password' => 'required|min:6',
'password_confirmation' => 'required|same:password'
]);
$current_password = $request->current_password;
$user = auth()->user();
if (Hash::check($current_password, $user->password)) {
$user->fill([
'password' => Hash::make($request->password)
])->save();
return redirect()->back()->with('success', 'Password Updated');
} else{
return redirect()->back()->with('error', 'Sorry a problem occurred while Updating this Password.');
}