尝试在laravel中重置密码时尝试获取非对象的属性

时间:2019-04-22 18:44:20

标签: laravel authentication passwords reset

我正在尝试重设密码,但是我无法解决我的问题,我陷入了这个问题,已经度过了几个小时,但是我不知道问题出在哪里:

我的控制器:

  class ResetPasswordController extends Controller
   {
protected $user;



public function __construct(User $user)
{
    // set the model
    $this->user = $user;

}
public function showResetForm(Request $request, $token = null)
{
    return view('auth.passwords.reset')->with(
        ['token' => $token, 'email' => $request->email]
    );
}

public function reset(Request $request)
{
    $validator = UserValidations::changePassword($request->all());

    if($validator->fails()) {
        return response(['status' => false,'message' => __('messages.validation_errors'), 'errors' => $validator->errors()->all()], 200);
    }

    try {
        $password = $this->user->where('id', Auth::user()->id)->value('password');

        if(Hash::check($request->input('current_password'),$password)) {

            $this->user->where('id', Auth::user()->id)->update(['password' => bcrypt($request->input('new_password'))]);

            $token = $request->header('Authorization');

            JWT::invalidate($token);

            Auth::logout();

            return response(['status' => true, 'message' => 'Password changed successfully'], 200);

        } else {
            return response(['status' => false, 'message' => 'The Current Password is invalid.'], 200);
        }
    } catch (\Exception $ex) {
        return response(['status' => false, 'message' => $ex->getMessage()], 500);
    }
}

}

我的观点:

                    <form class="form-horizontal" method="POST" action="{{ route('password.request') }}">
                    {{ csrf_field() }}

                    <input type="hidden" name="token" value="{{ $token }}">

                    <div class="form-group{{ $errors->has('current_password') ? ' has-error' : '' }}">
                        <label for="password" class="col-md-4 control-label">Current Password</label>

                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control" name="current_password" required>

                            @if ($errors->has('current_password'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('current_password') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('new_password') ? ' has-error' : '' }}">
                        <label for="password" class="col-md-4 control-label">Password</label>

                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control" name="new_password" required>

                            @if ($errors->has('new_password'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('new_password') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('new_password_confirmation') ? ' has-error' : '' }}">
                        <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
                        <div class="col-md-6">
                            <input id="password-confirm" type="password" class="form-control" name="new_password_confirmation" required>

                            @if ($errors->has('new_password_confirmation'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('new_password_confirmation') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                Reset Password
                            </button>
                        </div>
                    </div>
                </form>

但是它说要在非对象上获得财产,请对此提供帮助,我们将不胜感激 我还附上了我的第一个屏幕的屏幕截图,其中有3个输入字段,第二个屏幕错误 您的帮助需要

enter image description here

1 个答案:

答案 0 :(得分:0)

您是否在Route Facade / ServiceProvider中绑定了用户参数?

如果不这样做,则只需在路由password.request用户ID中添加一个位置,即可将用户ID隐式绑定到模型。您的错误很可能是因为您的__construct()的用户对象输出为空。