简体   繁体   中英

Laravel hash password in controller

I want to make a password hash inside the Controller so that the information is stored to the database.

My storing function is;

 public function store(Request $request)
{
    $validator = \Validator::make($request->all(), ['fullname' => 'required',
        'email'  => 'required|email|max:255|unique:users',
        'username'  => 'required|max:255|unique:users',
        'password'  => 'required',]);
    if ($validator->fails()) {

       return response()->json([
        '1' => 'Your information not stored!',
      ]);       
    }
    else {
    $newuser = new User;
    $newuser -> fullname = request('fullname');
    $newuser -> email = request('email');
    $newuser -> password = request('password');
    $newuser -> username = request('username');
    $newuser -> status = 1;
    $newuser -> role_id = 1;
    $newuser -> save();
    return response()->json([
        '2' => 'Your information stored successfuly!',
    ]);
    }

This works correctly. But, I want to hash the password.

https://laravel.com/docs/5.0/hashing
https://laracasts.com/discuss/channels/requests/how-to-hash-user-input-password-when-using-form-validation-in-form-request-laravel-5


 public function setPasswordAttribute($value)
{
    $this->attributes['password'] = bcrypt($value);
}

This does not work. Does any of you have any suggestions?

只是改变

 $newuser -> password = bcrypt(request('password'));

Change :

$newuser->password = request('password');

To:

$newuser->password = bcrypt(request('password'));

See the docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM