Laravel web Development Question Project (Change Password ) class 5






Route


Route::get('/password-change','ChangepasswordController@create');
Route::post('/password-change','ChangepasswordController@store');
//admin dashboard
Route::get('/dashboard','AdminController@dashboard');



Controller

 

ChangepasswordController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Sentinel;
class ChangepasswordController extends Controller
{
    public function create()
    {
        return view('authentication.change_password');
    }
    public function store(Request $request)
    {
         $request->validate([
             'old_password'=>'required',
    'password' => 'required|min:4',
    'confirm_password' => 'required|min:4',
]);
       $hasher=Sentinel::getHasher();
       $old_password=$request->old_password;
       $password=$request->password;
       $confirm_password=$request->confirm_password;
       $user=Sentinel::getUser();
       if (!$hasher->check( $old_password, $user->password)) {
       return redirect()->back()->with('error','old password not match');
       }
       elseif ($password != $confirm_password) {
           return redirect()->back()->with('error','New password and confirm password not match');
       }
       else{
           Sentinel::update($user,array('password'=>$password
           ));
           return redirect()->back()->with('success','Password change Successfully');
       }
    }
}

 

AdminController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function dashboard()
    {
        return view('admin.dashboard');
    }
}


Resource views

change_password.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Password change</title>
</head>
<body>


    @if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif


@if(Session()->has('error'))
      <div style="color: red;">
          {{Session()->get('error')}}
      </div>
    @endif

    @if(Session()->has('success'))
      <div style="color: green;">
          {{Session()->get('success')}}
      </div>
    @endif
 
  <form action="/password-change" method="POST">
   
    {{csrf_field()}}

    <input type="password" name="old_password" placeholder="Old Password">
    <input type="password" name="password" placeholder="new Password">
    <input type="password" name="confirm_password" placeholder="confirm  Password">

    <input type="submit" value="Password Change & Save ">

     
  </form>

</body>
</html>


Thanks

No comments

Powered by Blogger.