猿问

如何在 Laravel 的个人资料中更改电子邮件地址表单

我成功更改了密码,但我在更改电子邮件时遇到了麻烦,我认为这会更容易,但出于某种原因我想不出该怎么做。


旧邮件:


新邮箱:

确认新邮箱:


这是我想制作的形式,但无法解决 Controllers 功能。


这就是我试图解决它的方式


public function changeemailPost(Request $request)

    {


        if (!(Hash::check($request->get('email'), Auth::user()->email))) {

            return redirect()->back()->with("error","Your current e-mail does not matches with the e-mail you provided. Please try again.");

        }


        if(strcmp($request->get('email'), $request->get('new-email')) == 0){

            return redirect()->back()->with("error","New e-mail cannot be same as your current e-mail. Please choose a different e-mail.");

        }


            $validate = $request->validate([

                'email' => 'required',

                'new-email' => 'required|string|min:6|confirmed',

            ]);

            $user = Auth::user();

            $user->email = $request['new-email'];

            $user->save();

            Auth::logout();

            return view('auth.login');

        }

这是html


<form method="POST" action="{{ route('change-emailPost') }}">

                            @csrf

                            <label>Enter your old e-mail:</label>


                            <fieldset class="youplay-input">

                                <input id="current-email" type="email" class="form-control" placeholder="Current E-Mail" name="email" required>

                            </fieldset>


                            <label>Enter your new e-mail:</label>


                            <fieldset class="youplay-input">

                                <input id="new-email" type="email" placeholder="New E-Mail" class="form-control" name="new-email" required>

                            </fieldset>



但我只得到这个错误


您当前的电子邮件与您提供的电子邮件不匹配。请再试一次



ibeautiful
浏览 115回答 1
1回答

德玛西亚99

您需要更改您的changemailPost功能。尝试这个 -public function changeemailPost(Request $request){    if (!(Hash::check($request->get('email'), Auth::user()->email))) {        return redirect()->back()->with("error","Your current e-mail does not matches with the e-mail you provided. Please try again.");    }    if(strcmp($request->get('email'), $request->get('new-email')) == 0){        return redirect()->back()->with("error","New e-mail cannot be same as your current e-mail. Please choose a different e-mail.");    }        $validate = $request->validate([            'new-email' => 'required|string|min:6|confirmed',            'new-email_confirmation' => 'required|string|min:6',        ]);        $user = Auth::user();        $user->email = $request['new-email'];        $user->save();        Auth::logout();        return view('auth.login');    }你的 HTML 如下所示 -<form method="POST" action="{{ route('change-emailPost') }}">                        @csrf                        <label>Enter your old e-mail:</label>                        <fieldset class="youplay-input">                            <input id="current-email" type="email" class="form-control" placeholder="Current E-Mail" name="email" required>                        </fieldset>                        <label>Enter your new e-mail:</label>                        <fieldset class="youplay-input">                            <input id="new-email" type="email" placeholder="New E-Mail" class="form-control" name="new-email" required>                        </fieldset>                        <fieldset class="youplay-input">                            <input id="new-email-confirm" type="email" class="form-control" placeholder="Confirm New E-Mail" name="new-email_confirmation" required>                        </fieldset>                        <button type="submit" class="btn btn-default db">                            Change E-Mail                        </button>                    </form>如果要比较两个输入字段a & b的值和confirmed验证规则b字段应重命名为b_confirmation.
随时随地看视频慕课网APP
我要回答