How to Generate New Password in Laravel?
Step 1. Create a new Project in Laravel, so Open git Bash. Write down the following command:-
$ composer create-project --prefer-dist laravel/laravel changepassword "5.8.*"

Step 2. Now, Move to project directory on git Bash, so write down the following command:-
$ cd changepassword

Mysql Database connection Laravel
Step 3. Now, we have to Create a new Database. Go to XAMPP server->phpMyAdmin->Click New Database->changepassword.

Step 4. Now, Go to .env file to set the project path and give the project APP_URL, DB_DATABASE name and DB_USERNAME name.


Step 5. Create the user authentication scaffolding. Write down the following command:-
$ php artisan make:auth

Step 6. Thereafter, Migrate tables into the MySQL database. Write the following command within git Base.
$ php artisan migrate

Step 7. Go to app/Http/Controller/HomeController.php file and write down this following code:-
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Hash; | |
use Auth; | |
use Session; | |
class HomeController extends Controller | |
{ | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->middleware('auth'); | |
} | |
/** | |
* Show the application dashboard. | |
* | |
* @return \Illuminate\Contracts\Support\Renderable | |
*/ | |
public function index() | |
{ | |
return view('home'); | |
} | |
public function changePasswordUpdate(Request $request) | |
{ | |
if(!(Hash::check($request->get('current_password'),Auth::user()->password))){ | |
return back()->with('error','Your current password does not match what you provided'); | |
} | |
if(strcmp($request->get('current_password'), $request->get('new_password'))==0){ | |
return back()->with('error','Your new password cant be same with your current password'); | |
} | |
$request->validate([ | |
'current_password'=>'required', | |
'new_password'=> 'required|string|min:6|confirmed' | |
]); | |
$user = Auth::user(); | |
$user->password = bcrypt($request->get('new_password')); | |
$user->save(); | |
return back()->with('message','Password changed Successfully'); | |
} | |
} |
Step 8. Go to resources/views/home.blade.php file and write down this following code:-
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
@if ($errors->any()) | |
<div class="alert alert-danger"> | |
<ul> | |
@foreach($errors->all() as $error) | |
<li> | |
{{$error}} | |
</li> | |
@endforeach | |
</ul> | |
</div> | |
@endif | |
@if(session('error')) | |
<div class="alert alert-danger" role="alert"> | |
{{session('error')}} | |
</div> | |
@endif | |
@if(session()->get('message')) | |
<div class="alert alert-success" role="alert"> | |
<strong>Success: </strong>{{session()->get('message')}} | |
</div> | |
@endif | |
<div class="row justify-content-center"> | |
<div class="col-md-8"> | |
<div class="card"> | |
<div class="card-header">{{Auth::user()->name}}'s Dashboard</div> | |
<div class="card-body"> | |
@if (session('status')) | |
<div class="alert alert-success" role="alert"> | |
{{ session('status') }} | |
</div> | |
@endif | |
@if($message = Session::get('success')) | |
<div class="alert alert-success"> | |
<p>{{$message}}</p> | |
</div> | |
@endif | |
<form action="{{route('home')}}" method="POST"> | |
@csrf | |
<div class="form-group"> | |
<label for="current_password"><strong>Current Password:</strong></label> | |
<input type="password" class="form-control" id ="current_password" name="current_password"> | |
</div> | |
<div class="form-group"> | |
<label for="new_password"><strong>New Password:</strong></label> | |
<input type="password" class="form-control" id ="new_password" name="new_password"> | |
</div> | |
<div class="form-group"> | |
<label for="new_password_confirmation"><strong>Confirm New Password:</strong></label> | |
<input type="password" class="form-control" id ="new_password_confirmation" name="new_password_confirmation"> | |
</div> | |
<button class="btn btn-primary" type="submit">Generate New Password</button> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
@endsection | |
Step 9. Go to Route/web.php file and write down this following code:-
Route::post('/home', 'HomeController@changePasswordUpdate')->name('home');

Login


Checking the current password matches or not with the provided password

Checking new password should not be same with the current password

Password changed Successfully

Thanks



With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.