Hi, I am going to show how to update or change your password in laravel. So first I hope you already installed a laravel project.

First, create a page for updating the password.

change_password.blade.php and paste the below code. 

<div class="row">

                                            <h5>Change Password</h5>

                                            @if(count($errors))

                                                @foreach ($errors->all() as $error)

                                                <p class="alert alert-danger alert-dismissible fade show"> {{ $error}} </p>

                                                @endforeach

                                            @endif

                                            <form action="{{route('change.password')}}" method="POST">

                                                @csrf

                                                <div class="mb-3">

                                                    <label for="">Old Password</label>

                                                    <input type="text" name="old_password" class="form-control" placeholder="Old Password">

                                                </div>

                                                <div class="mb-3">

                                                    <label for="">New Password</label>

                                                    <input type="text" name="new_password" class="form-control" placeholder="New Password">

                                                </div>

                                                <div class="mb-3">

                                                    <label for="">Confirm Password</label>

                                                    <input type="text" name="confirm_password" class="form-control" placeholder="Confirm Password">

                                                </div>

                                                <div class="mb-3">

                                                    <label for=""></label>

                                                    <button class="btn btn-outline-success" type="submit">Change Password</button>

                                                </div>

                                            </form>

                                        </div>

       @if(count($errors))

                                                @foreach ($errors->all() as $error)

                                                <p class="alert alert-danger alert-dismissible fade show"> {{ $error}} </p>

                                                @endforeach

                                            @endif

This part using for showing the error.

Now create a controller and use it in the web.php file

php artisan make:controller User/ChangepasswordController

Once successfully create a controller go to the web.php file

use App\Http\Controllers\User\ChangepasswordController;

Route::get('/user-profile',[ChangepasswordController::class,'index']);

Route::post('/change-password',[ChangepasswordController::class,'changePassword'])->name('change.password');

Now go to the controller file and paste the below code.

Note: First Route will show the form and the second route will update the password 

<?php


 

namespace App\Http\Controllers\User;


 

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

use App\Models\User;

use Illuminate\Support\Facades\Hash;

class ChangepasswordController extends Controller

{

    public function index(){

        $profile = User::where('id',Auth::id())->first();

        return view('frontend.profile.index',compact('profile'));

    }//End Method


 

    public function changePassword(Request $request){

        $validateData = $request->validate([

            'old_password'=>'required',

            'new_password'=>'required',

            'confirm_password'=>'required|same:new_password',

        ]);

        $hashedPassword = Auth::user()->password;

        if(Hash::check($request->old_password,$hashedPassword)){

            $users = User::find(Auth::id());

            $users->password = bcrypt($request->new_password);

            $users->save();

            session()->flash('message','Password Updated Successfully');

            return redirect()->back();

        }else{

            session()->flash('message','Old password is not match');

            return redirect()->back();

        }

    }//End Method

}

Here function name index is a method that we set in a route previously. it will help to view the form and changePassword method will update the password which is also set in a route as a method.

We have to use,   use Illuminate\Support\Facades\Hash; and use Illuminate\Support\Facades\Auth; because we are using Auth for the checking of current user login password and id. and Hash will help to set the bcrypt password into the database.

Note: And don't forget to use Model User because we are updating the same on the users table.

 

Leave a Comment
No Comments found