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

问题描述 投票:0回答:1

我正在尝试重置我的密码,但我无法解决我的问题,我陷入了这个问题并且已经过了几个小时但我无法弄清楚问题出在哪里:

我的控制器:

  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

laravel authentication passwords reset
1个回答
0
投票

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

如果你不这样做,只需添加一个地方就可以将password.request用户id路由到隐式模型绑定。您的错误很可能是因为您的__construct()有一个空的用户对象输出。

© www.soinside.com 2019 - 2024. All rights reserved.