Hello to all, welcome to therichpost.com. In this post, I will tell you, Laravel 6 change user password complete working code.
Post Working:
In this post, I am doing, login user can change his/her password and I am doing this in Laravel 6. I will share, blade, route and controller code in this post.
Here is the working code snippet and please use carefully:
1. Here is the blade template file code:
@if(session()->has("message")) <div class="alert alert-success"> <p> {{session("message")}} </p> </div> @endif @foreach ($errors->all() as $error) <div class="alert alert-danger"> <p>{{ $error }}</p> </div> @endforeach <form action="{{ URL::to('/change-password') }}" method="post"> {{ csrf_field() }} <div class="form-group"> <label for="image"><strong>Change Password:</strong></label> <input type="password" class="form-control" id="password" placeholder="New Password" name="password" required> <input type="password" name="objectId" value="{{ encrypt(Auth::user()->id) }}" style="display: none;"> </div> <button type="submit" class="btn btn-warning">Submit</button> </form>
2. Here is the route code:
Route::post('/change-password','HomeController@changePassword');
3. Here is the controller code:
... use Validator; ... public function changePassword(Request $request) { $validator = Validator::make($request->all(), [ 'password' => 'required|max:255' ]); if ($validator->fails()) { return redirect()->back() ->withErrors($validator) ->withInput(); } $reset_password = DB::table('users')->where('id',decrypt(Input::get('objectId')))->update(['password' => bcrypt(Input::get('password'))]); return Redirect::to('home')->with("message","Successful!! Password has been changed"); }
This is it, If you have any query then please do comment below.
Jassa
Thank you
Why dont you use eloquent and where is validation before update
Yes right and I will update it.
There is no need to send the user_id.
since only the logged in user can change his password.
I just made it for custom and yes you are also right.