当使用模式在 laravel 中更改用户密码时,它总是 return false ajax?

when change user password in laravel by using modal, it always return false ajax?

我正在尝试实现 (changePassword) 方法,使用户能够更改他们的旧密码,我为此使用了 (modal) 和 ajax ,就像那样:

数据来自 ajax 到(控制器)没问题,但是:

路线:

    Route::group(['middleware'=>'auth:web'], function(){
    Route::get('/profile', [ProfileController::class,'profile'])->name('profile');
    Route::post('/change-password', [ProfileController::class,'changePassword'])->name('profile.changePassword');
});

控制器:

    public function changePassword(ProfilePasswordRequest $request)
{
    try{
        $currentPass = Auth::user()->password;
        if (Hash::check($request->oldPassword, $currentPass)) {
            $user = User::find(Auth::id());
            $user -> password = Hash::make($request->password);
            $user -> save();

            return response()->json([
                'status' => true,
                'msg' => 'Your password changed successfully',
            ]);
        }else{
            return response()->json([
                'status' => false,
                'msg' => 'Old password is wrong',
            ]);
        }

    }catch (\Exception $ex){
        return $ex;
        return redirect()->back()->with(['error' => 'Something error please try again later']);
    }
}

总是 return 响应转到 (else),尽管(来自模态的旧密码)与存储在数据库中的密码相同?

  'status' => false,
  'msg' => 'Old password is wrong',

更改密码请求:

    <?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ProfilePasswordRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'oldPassword' => 'required',
            'password' => 'required|confirmed|min:8',
        ];
    }
}

脚本:

    <script>
    $(document).on('click', '#changePassword', function(e){
        $("#changePassword").attr("disabled", true);
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $('#oldPassword_error').text('');
        $('#password_error').text('');

        var password = $('#password').val();
        var oldPassword = $('#oldPassword').val();
        var passwordConfirmation = $('#password_confirmation').val();
        console.log(oldPassword);

        $.ajax({
            type: 'post',
            url: "{{route('profile.changePassword')}}",
            data:{
                oldPassword:oldPassword,
                password:password,
                password_confirmation:passwordConfirmation
            },
            cache: false,
            success: function (response){

                if(response.status===true){
                    $('#changPassForm')[0].reset();
                    $('#ChangePasswordMsgSucc').show();
                    $("#changePassword").attr("disabled", false);
                }
                if(response.status===false){
                    $('#changPassForm')[0].reset();
                    $('#ChangePasswordMsgError').show();
                    $("#changePassword").attr("disabled", false);
                }

            }, error: function (reject){
                $("#changePassword").attr("disabled", false);
                var response = $.parseJSON(reject.responseText);
                $.each(response.errors, function(key, val){
                    $("#" + key + "_error").text(val[0]);
                });
            }
        });
    });
</script>

网页:

            <div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel2" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">

          <div  class="row mr-2 ml-2">
              <button id="ChangePasswordMsgError" style="display: none" type="button" class="btn btn-lg btn-block btn-outline-danger mb-2"
                      >Your old password is wrong
              </button>
          </div>

          <div  class="row mr-2 ml-2">
              <button id="ChangePasswordMsgSucc" style="display: none" type="button" class="btn btn-lg btn-block btn-outline-danger mb-2"
              >Your old password is wrong
              </button>
          </div>

           <div class="row  d-flex justify-content-center " style="font: normal normal bold 24px/45px Cairo; color: #0D67CB">

    <p class="text-center">Change your Password</p>

    </div>
          <form id="changPassForm">
              @csrf
        <div class="row mt-3 pl-3 pr-3 mr-3 ml-3 d-flex justify-content-center">
           <input type="password" id="oldPassword" class="form-control" placeholder="Old Password">
            <small id="oldPassword_error" class="form-text text-danger"></small>
        </div>
        <div class="row mt-3 pl-3 pr-3 mr-3 ml-3 d-flex justify-content-center">
           <input id="password" type="password" class="form-control" placeholder="New Password">
            <small id="password_error" class="form-text text-danger"></small>
        </div>
        <div class="row mt-3 pl-3 pr-3 mr-3 ml-3 d-flex justify-content-center">
           <input id="password_confirmation" type="password" class="form-control" placeholder="Confirm New Password">
        </div>
          </form>

      </div>
      <div class="modal-footer pr-5 pt-5 pb-5">
       <button type="button" class="btn btn-light">Reset</button>
       <button id="changePassword" type="button" class="btn btn-warning">Change</button>

      </div>
    </div>
  </div>
</div>

我尝试 console.log() 所有来自 ajax 的值,结果成功了。

请帮忙。

我不是 100% 确定这是问题所在,但是请求变量是否可以使用 -> 表示法访问。或者你必须调用 ->get('name') 或 ->input('name')

if (Hash::check($request->get('oldPassword'), $currentPass)) {
        $user = User::find(Auth::id());
        $user->password = Hash::make($request->get('password'));
        $user->save();

        return response()->json([
            'status' => true,
            'msg' => 'Your password changed successfully',
        ]);
    } else {
        return response()->json([
            'status' => false,
            'msg' => 'Old password is wrong',
        ]);
    }