Laravel 9 Change Password with Current Password Validation Tutorial

Apr 29, 2022 . Admin

Hello Friends,

Now let's see an example of how to change a password with current password validation in laravel 9. This is a short guide on laravel 9 if change password with current password validation. We will use how to use change password with current password validation in laravel 9. Here you will learn how to use change password with current password validation in laravel 9. Let's get started with how to change password with current password validation in laravel 9.

Here i will give you many example how you can change password with current password validation in laravel 9.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Create Migration and Model

We have to create migration for "admin" table using Laravel 9 php artisan command, so first fire bellow command:

php artisan make:migration create_admin_table

After, you will find one file in following path "database\migrations" and you have to put bellow code in your file for create files table

database\migrations\create_admin_table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAdminTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admin', function (Blueprint $table) {
            $table->increments('id');
            $table->text('name');
            $table->string('email');
            $table->string('password');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admin');
    }
}

Now you can run migration by following command :

php artisan migrate

Now, we have to create a model so fire bellow command:

php artisan make:model Admin

Now go to model and paste this following code.

app\Models\Admin.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Admin extends Model
{
    use HasFactory;

    public $table = 'admin';
   
    public $fillable = ['name','email','password'];
}
Step 3: Create Route

we have to add route using resource controller.

routes\web.php

<?php

use App\Http\Controllers\SettingsController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::group(['middleware'=>'auth' ,'prefix'=>'admin'], function() {    
    Route::resource('settings', SettingsController::class);
});
Step 4: Create Controller
php artisan make:controller SettingsController

Now go to controller and paste this following code.

app\Http\Controllers\SettingsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use Illuminate\Support\Facades\Auth;

class SettingsController extends Controller
{
    /**
    * Write code on Method
    *
    * @return response()
    */
    public function edit($id)
    {
        $users = admin::find(Auth::user()->id);
        return view('settings',compact('users'));
    }
    
    /**
    * Write code on Method
    *
    * @return response()
    */
    public function update(Request $request, $id)
    {
        $this->validate($request, [ 
            'oldpassword' => 'required',
            'newpassword' => 'required',
        ]);
 
        $hashedPassword = Auth::user()->password;
        if (\Hash::check($request->oldpassword , $hashedPassword)) {
            if (\Hash::check($request->newpassword , $hashedPassword)) {
 
                $users = admin::find(Auth::user()->id);
                $users->password = bcrypt($request->newpassword);
                $users->save();

                session()->flash('message','password updated successfully');
                return redirect()->back();
            }
            else{
                session()->flash('message','new password can not be the old password!');
                return redirect()->back();
            } 
        }
        else{
            session()->flash('message','old password doesnt matched');
            return redirect()->back();
        }
    }
}
Step 5: Create Blade File

Now paste the following code to your settings.blade.php file.

resources\views\settings.blade.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Laravel 9 Change Password with Current Password Validation Tutorial</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    </head>
    <body>
        <div class="container mt-5">
            <div class="row">
                <div class="col-md-6 offset-md-3">
                    <div class="card">
                        <div class="card-header">
                            <h6>Laravel 9 Change Password with Current Password Validation</h6>
                        </div>
                        <div class="card-body">
                            @if (count($errors))
                                @foreach ($errors->all() as $error)
                                    <p class="alert alert-danger">{{$error}}</p>
                                @endforeach
                            @endif    
                            <form id="demo-form2" data-parsley-validate class="form-horizontal form-label-left" action="{{ route('settings.update',[$users->id,$users->slug]) }}" method="post">
                                @csrf
                                @method('PATCH')
                                <div class="form-group">
                                    <label>Enter Old Password :</label>
                                    <input type="password" id="first-name" class="form-control"  placeholder="Enter old password" name="oldpassword"> 
                                </div>
                                <div class="form-group">
                                    <label>Enter New Password :</label>
                                    <input type="password" id="first-name" placeholder="Enter new password" class="form-control" name="newpassword"> 
                                </div>
                                <div class="form-group">
                                    <label>Enter Confirm Password :</label>  
                                    <input type="password" id="first-name"  class="form-control"placeholder="Enter password confirmation"  name="password_confirmation"> 
                                </div>
                                <button type="submit" class="btn btn-primary">Update</button>
                            </form> 
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/admin/settings

It will help you....

#Laravel 9