Laravel 8 Change Password with Current Password Validation

Laravel 8 Change Password with Current Password Validation

In this example we will show how to change password with current password validation in laravel 8. You will learn laravel if change password with current password validation. We will explain step by step how to use change password with current password validation in laravel 8.

Here, we will learn how you can change password with current password validation in laravel, change password with old password validation rule in laravel 8, Laravel old password validation, Change password in Laravel 8.

Step 1 : Install Laravel 8

First step we required to get fresh laravel 8 version application So let’s open terminal and run following command to install fresh laravel project.

composer create-project --prefer-dist laravel/laravel blog

Step 2 : Create Migration and model

We have to create migration for “admin” table using Laravel 8 php artisan command, so first fire following command :

php artisan make:migration create_admin_table

Path: 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 bellow command :

php artisan migrate

Now, we have to create model so fire following command :

php artisan make:model Settings

Path: 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 : Add Route

<?php
  use App\Http\Controllers\SettingsController;

  Route::group(['middleware'=>'auth' ,'prefix'=>'admin'], function() {  
    Route::resource('settings', SettingsController::class);
  });
?>

Step 4 : Create Controller 

php artisan make:controller SettingsController

Path: 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

Path: resources\views\settings.blade.php
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Laravel 8 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 8 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>

Now run following command for quick run:

Read Also :  Laravel 8 Multi Step Form Example
php artisan serve
Now open following URL on your browser:
localhost:8000/admin/settings
I hope you understand of change old password with current password validation in Laravel 8 and it can help you..