如何更改密码?

How to change password?

我只想编辑表单更新 addressemailpassword。如何更改密码?旧密码很重要。

edit.blade.php

<form method="POST" action="{{ route('update') }}">
    @csrf
    {{ method_field('PATCH') }}
    <div class="form-group row">
        <label for="email" class="col-md-1 col-form-label text-md-right">{{ __('Email') }}</label>

        <div class="col-md-5">
            <input id="email" type="text" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') ? : user()->email }}" required autocomplete="email" autofocus>

            @error('email')
            <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
    </div>

    <div class="form-group row">
        <label for="password" class="col-md-1 col-form-label text-md-right">{{ __('Password') }}</label>

        <div class="col-md-5">
            <input id="password" type="text" class="form-control @error('password') is-invalid @enderror" name="password" value="{{ old('password') }}" required autocomplete="password" autofocus>

            @error('password')
            <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
    </div>

    <div class="form-group row">
        <label for="address" class="col-md-1 col-form-label text-md-right">{{ __('Address') }}</label>

        <div class="col-md-5">
            <textarea id="address" type="text" class="form-control @error('address') is-invalid @enderror" name="address" required autocomplete="address" autofocus>{{ old('address') ? : user()->address }}</textarea>

            @error('address')
            <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
    </div>

    <div class="form-group row mb-0">
        <div class="col-md-1">
            <button type="submit" class="btn btn-block btn-primary">
                {{ __('Register') }}
            </button>
        </div>
    </div>
</form>

路线

Route::post('update', 'Auth\RegisterController@sqlupdate')->name('update');

注册控制器

public function sqlupdate(Request $request)
{
    Auth::user()->update([
        'address' => $request['address'],
        'email' => $request['email'],
    ]);

    $hashedPassword = auth()->user()->password;
    if (Hash::check($request->oldpassword, $hashedPassword)){
        $user = User::findOrFail(Auth::id());
        $user->password = Hash::make($request->password);
    }

    return redirect()->back();
}

请仔细阅读以下代码:-

/**
 * Admin My profile : Password update.
 *
 * @param Request $request
 * @param $id
 * @return \Illuminate\Http\Response
 */
public function updatePassword(Request $request,$id = 0)
{
    $validate = Validator::make($request->all(),[
        'old_password'          => 'required',
        'password'              => 'required|confirmed|min:8',
        'password_confirmation' => 'required|min:8',
        ]);
    $getUserData = Admin::where('id',$id)->first();
    if($getUserData === null) {
        return  redirect()->back()->with([
            'status'    => 'warning',
            'title'     => 'Warning!!',
            'message'   => 'Invalid Admin ID.'
            ]);
    }
    $validate->after(function ($validate) use ($request,$getUserData,$id) {
       if(!Hash::check($request->get('old_password'),$getUserData->password)){
           $validate->errors()->add('old_password', 'Wrong old password');
       }
   });
    if($validate->fails()){
        return redirect()->back()->withErrors($validate)->withInput();
    }
    try{
        $getUserData->update([
            'password'  => Hash::make($request->get('password'))
            ]);
        return  redirect()->back()->with([
            'status'    => 'success',
            'title'     => 'Success!!',
            'message'   => 'Admin password updated successfully.'
            ]);
    }catch (Exception $e){
        return  redirect()->back()->with([
            'status'    => 'error',
            'title'     => 'Error!!',
            'message'   => $e->getMessage()
            ]);
    }
}

通过上述方法,您将了解我们如何更新密码,这是我的一个项目,我为此创建了三个字段,这里是视图的屏幕截图:-

希望对您有所帮助

Further more update here is the small snippet for update method specially

 $getOldPassword = User::where('id',$id)->first();
 if($request->get('password') === null){
     $password = $getOldPassword->password;
 }else{
     $password = Hash::make($request->get('password'));
 }