Laravel 8 Change Password with Current Password Validation Tutorial

Laravel 8 , Laravel 7 , Laravel , Laravel 6

Nicesnippets

1182

28-09-2021


Laravel 8 Change Password with Current Password Validation Tutorial

Hello Friends,

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

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

Step 1 : Install Laravel 8


we need to get fresh laravel 8 version application So let's open terminal and run bellow 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 bellow command :

php artisan make:migration create_admins_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 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 : Add Route

we have to add route using resource controller.

routes\web.php

<?php

use App\Http\Controllers\SettingsController;

Route::group(['middleware'=>'auth' ,'prefix'=>'admin'], function() {

Route::resource('settings', SettingsController::class);

});

Step 4 : Create Controller and Model

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 8 Change Password with Current Password Validation Tutorial - NiceSnippets.com</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 - NiceSnippets.com</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 bellow command for quick run:

php artisan serve

Now open bellow URL on your browser:

localhost:8000/admin/settings

It will help you....


Recommended Posts