在Laravel中难以将旧电子邮件更改为新电子邮件

时间:2019-02-15 04:49:09

标签: php laravel laravel-5

我很难将当前用户的电子邮件更改为新的电子邮件,这是用户输入和电子邮件的验证。已编辑 这是我的控制器

 public function changeuser(Request $request){
  $id = Auth::user()->id;
  $change_user = User::find($id);

  $valid = validator($request->only('oldpass', 'newpass', 'confirmpass'), [
  'olduser' => 'required|email|max:255|exists:users',
  'newuser' => 'required|email|max:255|different:olduser'
   ], [
    'newuser.required_with' => 'Choose different email.'
 ]);

  if ($valid->fails()) {
      return redirect('/manageaccount')->with('message','Failed to update email');
      }

  $change_user->email = $request->input('newuser');
  $change_user->save();

  return redirect('/manageaccount')->with('message', 'email changed successfully');

}

我的blade.php是模态的

<div class="thirdea" id="thirdea">

             <label><p class="small Montserrat">Enter Old Username</p></label>
            <input type="text" class="form-control small Montserrat" name="olduser" value="" id="olduser">

            <label><p class="small Montserrat"> New Username</p></label>
            <input type="text" class="form-control small Montserrat" name="newuser" value="" id="newuser">

        </div>

我的路线

Route::post('/changeuser/','UserController@changeuser');

1 个答案:

答案 0 :(得分:0)

对于更新用户,您可以尝试对电子邮件进行验证,如下所示:

 $validate = $request -> validate([
      'newuser' => 'required|unique:users,email,'.$user->id
 ]);