更改密码功能在Laravel 6中不起作用

问题描述

我是Laravel和PHP的新手(一周前刚进入这家新公司)。我正在尝试使用Laravel 6构建“更改密码”功能,但是由于某些原因,它根本无法正常工作。它一直告诉我“当前密码字段与您的密码不匹配”,即使我确定我的当前密码是正确的。我问我的年长者,他也不知道为什么会笑

这是用户编辑密码的前端部分:

<div class="card">
                <div class="card-header">
                    <h5 class="title">{{ __('Password') }}</h5>
                </div>
                <form method="post" action="{{ route('profile.password',$user->id) }}" autocomplete="off">
                    <div class="card-body">
                        @csrf
                        @method('put')

                        @include('alerts.success',['key' => 'password_status'])

                        <input type="hidden" name="id" value="{{$user->id}}">
                        <div class="form-group{{ $errors->has('old_password') ? ' has-danger' : '' }}">
                            <label>{{ __('Current Password') }}</label>
                            <input type="password" name="old_password" class="form-control{{ $errors->has('old_password') ? ' is-invalid' : '' }}" placeholder="{{ __('Current Password') }}" value="" required>
                            @include('alerts.Feedback',['field' => 'old_password'])
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-danger' : '' }}">
                            <label>{{ __('New Password') }}</label>
                            <input type="password" name="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" placeholder="{{ __('New Password') }}" value="" required>
                            @include('alerts.Feedback',['field' => 'password'])
                        </div>
                        <div class="form-group">
                            <label>{{ __('Confirm New Password') }}</label>
                            <input type="password" name="password_confirmation" class="form-control" placeholder="{{ __('Confirm New Password') }}" value="" required>
                        </div>
                    </div>
                    <div class="card-footer">
                        <button type="submit" class="btn btn-fill btn-primary">{{ __('Change password') }}</button>
                    </div>
                </form>
            </div>
        </div>

这是控制器内更改密码的功能

public function password(PasswordRequest $request,$id)
    {
        $user = Users::findOrFail($id);
        $user->password = Hash::make($request->get('password'));

        $user->save();
        
        return back()->withPasswordStatus(__('Password successfully updated.'));
    }

这是PasswordRequest(我没有写这部分。它与我正在使用的模板一起提供):

class PasswordRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return auth()->check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'old_password' => ['required','min:4',new CurrentPasswordCheckRule],'password' => ['required','confirmed','different:old_password'],'password_confirmation' => ['required','min:4'],];
    }

    /**
     * Get the validation attributes that apply to the request.
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'old_password' => __('current password'),];
    }
}

这是CurrentPasswordCheckRule:

class CurrentPasswordCheckRule implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute,$value)
    {
        return Hash::check($value,auth()->user()->password);
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return __('The current password field does not match your password');
    }
}

如果在注册阶段出现问题,这是注册代码

public function store(UserRequest $request,Users $model)
    {

        $user = new Users();

        $user->email = request('email');
        $user->last_name = request('last_name');
        $user->first_name = request('first_name');
        $user->company_id = request('company_id');

        //image
        $request->validate([
            'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',]);
  
        $imageName = time().'.'.$request->image->extension();  

        $upload_path = 'images/';
        $profile_image_url = $upload_path. $imageName;
        $request->image->move(public_path('images'),$imageName);

        $user->profile_image = $profile_image_url;
        //image

        $user->password = Hash::make($request->get('password'));

        $user -> save();


        return redirect()->route('pages.user_list')->withStatus(__('User successfully created.'));
    }

对不起,我知道这里有很多代码。在此先感谢您的帮助。问我任何问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)