Laravel is a popular PHP framework that provides many functions, including changing user passwords. In this article, we will introduce how to change user password in Laravel. The following are the specific steps:
Step 1: Create a route for changing passwords
First, we need to create a Laravel route for changing user passwords. You can add the following code in the web.php file:
Route::get('/password', 'UserController@password')->name('password'); Route::post('/password', 'UserController@updatePassword')->name('updatePassword');
Here we define a GET and POST route. The GET route is used to display the form for changing the password, and the POST route is used to submit the form data and perform the password update operation. At the same time, UserController@password and UserController@updatePassword are the two controller methods we need to create.
Step 2: Create a form to change the password
In the resources/views directory, we create a password.blade.php view file, which contains the form for updating the password. The following is a sample code:
<form method="POST" action="{{ route('updatePassword') }}"> {{ csrf_field() }} <div class="form-group"> <label for="current-password">Current Password</label> <input type="password" name="current-password" id="current-password" class="form-control" required> </div> <div class="form-group"> <label for="new-password">New Password</label> <input type="password" name="new-password" id="new-password" class="form-control" required> </div> <div class="form-group"> <label for="confirm-password">Confirm New Password</label> <input type="password" name="confirm-password" id="confirm-password" class="form-control" required> </div> <button type="submit" class="btn btn-primary">Submit</button> </form>
There are three input boxes for entering the current password, new password and confirming the new password. The form's submission address is set to the route name we defined in the route.
Step 3: Create controller methods
Now we need to create two methods in the UserController controller: password and updatePassword. The following is the sample code:
public function password() { return view('password'); } public function updatePassword(Request $request) { $this->validate($request, [ 'current-password' => 'required', 'new-password' => 'required|string|min:6|confirmed' ]); $user = Auth::user(); $currentPassword = $user->password; $passwordMatches = Hash::check($request['current-password'], $currentPassword); if ($passwordMatches) { $user->password = Hash::make($request['new-password']); $user->save(); return redirect()->back()->with('success', 'Password updated successfully!'); } else { return redirect()->back()->withErrors(['current-password' => 'Incorrect current password.']); } }
In the updatePassword method, we first verify that the form input complies with the rules. Then, we obtain the current user data through the Auth::user() method, and then verify whether the current password matches the password in the database through the Hash::check() method. If the password matches successfully, we will hash the new password using the Hash::make() method and save it to the database.
Step 4: Test the password change function
Now we have created the route, form and controller method. We can access the password change page by accessing the localhost/password address and test the code we wrote. If all goes well, we can let users change their passwords!
Conclusion
It is very convenient to change user passwords in Laravel. We only need to create a form, route and controller method to achieve this functionality. I hope this article will help you change user passwords in Laravel.
The above is the detailed content of Let's talk about how to change user password in Laravel. For more information, please follow other related articles on the PHP Chinese website!