Hi,
In this tutorial we will learn to how to change password in laravel command with checking old password in laravel application. we will explain to how to change-password in laravel.we will do old password verification using custom validation rule in laravel 6,laravel 7,laravel 8, laravel 9 and laravel 10.
In this tutorial we will create example from step by step. first we will create change password page. then that we will create our custom validation rules for checking with current password on database. then we implement custom validation rule on our controller file.
So, you need to just follow few step to get complete guide for how to change password in laravel and old password using custom validation in laravel.
we need to get new laravel application using below command.
Following step how to change password in laravel :
Step 1 : Install Laravel project
composer create-project laravel/laravel test
Step 2 : Create Custom Validation Rule
Now we need to create new validation rules using following laravel command:
php artisan make:rule CheckOldPassword
Then this we can see they created new Rules folder with one file an app directory.
app/Rules/CheckOldPassword.php
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Hash;
class CheckOldPassword implements Rule
{
public function passes($attributes,$value)
{
return Hash::check($value,auth()->user()->password);
}
public function message()
{
return 'The :$attributes is match with old password.';
}
}
Step 3 : Create Routes
Here, we need to add two route for view and another for post route.open your routes/web.php file and add following route.
routes/web.php
Route::get('change-password',[ChangePasswordController::class,'index']);
Route::post('change-password',[ChangePasswordController::class,'changePassword'])->name(change.password);
app/Http/Controllers/ChangePasswordController.php
<php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Rules\CheckOldPassword;
use Illuminate\Support\Facades\Hash;
use App\User;
class ChangePasswordController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('changePassword');
}
public function changePassword(Request $request)
{
$request->validate([
'current_password' => ['required',new CheckOldPassword],
'new_password' => 'required',
'new_confirm_password' => ['same:new_password'],
]);
USer::find(auth()->user()->password)->update(['password' => Hash::make($request->new_password)]);
dd('Password change successfully');
}
}
Step 4 : Create Blade File
In this step, we need to create changePassword blade file and we will write code for create and display error message.let’s start create file and put bellow code:
resources/views/changePassword.blade.php
<@extends('layout.main') @section('content') <div class="container">
<div class="row justify-content-center"></div>
<div class="col-md-10">
<div class="card">
<div class="card-header">
Laravel Change Password
</div>
<div class="card-body">
<form method="POST" action="{{ route('change.password') }}">
@csrf
@foreach ($errors->all() as $error)
<p class="text-danger">{{ $error }}</p>
@endforeach
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">Current Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="current_password"
autocomplete="current-password">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">New Password</label>
<div class="col-md-6">
<input id="new_password" type="password" class="form-control" name="new_password"
autocomplete="new-password">
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">Confirm Password</label>
<div class="col-md-6">
<input id="confirm_password" type="password" class="form-control" name="confirm_password"
autocomplete="confirm_password">
</div>
</div>
</div>
</div>
</div>
</div>
Read also: How to Create Seeder in Laravel
Now we are ready for run our laravel application.quick run following command:
php artisan serve
I hope this tutorial help you…