Laravel用户更新问题

时间:2016-11-15 22:39:46

标签: laravel authentication

我会尽可能地清楚地说明这一点。

我推出了一个make Auth调用,以便使用laravel的登录和注册功能,之后只使用模板来提供我想要的需求。

  1. 如果用户是管理员,他/她可以注册新用户。

    public function openNewUser(){
    return view('auth.register');
    

    }

    NB。部分更新。

    public function registerNewUser(Request $request){
            $this->validate($request,[
                'email' => 'required|email|unique:users',
                'name' => 'required|max:120',
                'password' => 'required|min:4|confirmed']);
    
            $user = new User();
    
            $user->name = $request->name;
            $user->email = $request->email;
            $user->password = encrypt($request->password);
    
            if (Gate::denies('register-user')) {
                return redirect()->back();
            }
    
            $user->save();
            return view('home');
      }
    
    1. 问题1 - 我还想更新用户,这会产生问题。密码输入返回空字段,我理解。当我尝试更改它时,确认密码总是给出不匹配,即使它们是相同的。当我把它留空时它也不起作用,因为需要填充场。我把它们从表格上取下来试试,如果我只能编辑电子邮件,但只是没有用。
  2. public function userUpdate(Request $ request,$ user_id){

      $this->validate($request,[
          'email' => 'required|email',
          'name' => 'required|max:120',
          'password' => 'required|min:4|confirmed']);
           $user = new User();
             $user->name = $request->name;
             $user->email = $request->email;
             $user->password = encrypt($request->password);
    
        if (Gate::allows('register-user')) {
              $user->save();
    
            $user->roles()->attach($request->roles);
            return redirect()->route('view_users');
        }elseif (Gate::denies('register-user')) {
            if (Auth::id() == $user_id) {
                $user->save();
    
                $user->roles()->attach($request->roles);
                return redirect()->route('view_users');
            }else{
                return redirect()->back();
            }
        }
    
    }
    

    问题2.我刚刚意识到我正在使用我的新注册进行的所有登录都提供这些凭据与我们的记录不匹配。即使凭据存在并且已正确注册。 我正在使用laravel提供的登录信息,但我创建了自己的注册。

    请如何编辑和更新我的用户,以及注册后能够登录

3 个答案:

答案 0 :(得分:1)

// Profile Update author 
  public function UpdateProfile(Request $request)
     {

        $this->validate($request,[
            'name' => 'required',
            'email' => 'required|email',
            'image' => 'mimes:jpeg,jpg,bmp,png',
        ]);
        $image = $request->file('image');
        $slug = str_slug($request->name);
        $user = User::findOrFail(Auth::id());
        if($request->hasfile('image'))
     {
        $image=$request->file('image');
        $currentDate = Carbon::now()->toDateString();
         $imageName = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
            $image_resize = Image::make($image->getRealPath());   
            $image_resize->resize(500,400);
        unlink('storage/uploads/author/profile/'.$user->image);

        $image_resize->save('storage/uploads/author/profile/'.$imageName);
        } else {
            $imageName = $user->image;
        }
        $user->name = $request->name;
        $user->email = $request->email;
        $user->image = $imageName;
        $user->about = $request->about;
        $user->save();
        Toastr::success('Profile Successfully Updated :)','Success');
        return redirect()->back();
     }
//Password Update

 public function passwordUpdate(Request $request)
    {
        $this->validate($request,[
            'old_password' => 'required',
            'password' => 'required|confirmed',
        ]);
        $hashedPassword = Auth::user()->password;
        if (Hash::check($request->old_password,$hashedPassword))
        {
            if (!Hash::check($request->password,$hashedPassword))
            {
                $user = User::find(Auth::id());
                $user->password = Hash::make($request->password);
                $user->save();
                Toastr::success('Password Successfully Changed','Success');
                Auth::logout();
                return redirect()->back();
            } else {
                Toastr::error('New password cannot be the same as old password.','Error');
                return redirect()->back();
            }
        } else {
            Toastr::error('Current password not match.','Error');
            return redirect()->back();
        }
    }

答案 1 :(得分:0)

您使用的是什么版本的Laravel?

以下是register()中的{v5.3} RegisterController.php方法,至少部分用于注册:

public function register(Request $request)
{   
...
// save and login user
$user = $this->create($request->all());
$this->guard()->login($user);
...
}
...
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'lastname' => $data['lastname'],
        'phone' => $data['phone'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

以及login()

中的LoginController.php方法
public function login(Request $request) 
{
$credentials = $this->credentials($request);
...
if ($this->guard()->attempt($credentials, $request->has('remember'))) {
      return $this->sendLoginResponse($request);
   }
}

希望我没有错过任何东西。 请记住,5.2版本已经发生了变化。

答案 2 :(得分:0)

我发现了什么问题,因为我使用Laravel的登录,我的注册必须使用bycrypt进行加密,这是Laravel注册使用的,但我在创建自己的注册时使用加密,所以当发生冲突时登录。(Remembere我使用Laravels登录而不是我自己写的登录)。我希望这有助于某人