Nasser Ali Karimi
Nasser Ali Karimi

Reputation: 4663

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?

Upvotes: 2

Views: 6016

Answers (2)

Sapnesh Naik
Sapnesh Naik

Reputation: 11656

Change:

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

To:

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

See the docs

Upvotes: 0

Kuldeep Mishra
Kuldeep Mishra

Reputation: 4040

just change

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

Upvotes: 3

Related Questions