iGenezys's avatar

Encrypt a password in database update

Hi guys, sorry for my english. I have a problem in a Laravel project, I want to update an user's password, but when I do it, the password is not encrypted. I put here my update code :

public function update($id, Request $request){

        $user = User::whereId($id)->first();

        $mdpuser = $request->input('passwordactu');

        $verifypass = password_verify($mdpuser, $user->password);

        if ($verifypass == true)
        {
            $this->validate($request, [

                'name' => "required|max:255|alpha_num|unique:users,name,{$user->id}",

            ]);

            $user->update($request->only('name', 'nom', 'prenom', 'role', 'password'));

            return Redirect::route('gestionUser.index')->with('success', 'La modification a correctement été effectuée');

        }else{

            return Redirect::route('gestionUser.index')->with('error', 'Il y a eu une erreur, merci de recommencer');
        }

    }

I try somethind like put a "bcrypt('password')" in my Update line, but in that case, the password is not updated in the database. Here, it's updated, but not encrypted.

Do you have an idea that can help me here ? Thank you !

0 likes
4 replies
Snapey's avatar

Probably easier to understand in the future if you pass an array into the update method rather than just the request object. You can then adjust any values as required - including the password encryption;

$user->update([
    'name' => $request->name,
    'nom' => $request->nom,
    'prénom' => $request->prénom,
    'role' => $request->role,
    'password' => bcrypt($request->password)
    ]);
2 likes
iGenezys's avatar

Oh, that's perfect, I can do things like that, thank you very much ! :)

BillRiess's avatar

I would suggest using Hash::make($request->password) instead with the upcoming helper function changes.

Please or to participate in this conversation.