控制器中的Laravel哈希密码

时间:2018-01-29 09:25:28

标签: php laravel hash

我想在Controller中创建一个密码哈希,以便将信息存储到数据库中。

我的存储功能是;

 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!',
    ]);
    }

这可以正常工作。但是,我想哈希密码。

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);
}

这不起作用。你们有没有任何建议?

2 个答案:

答案 0 :(得分:0)

只需更改

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

答案 1 :(得分:0)

更改

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

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

请参阅docs