Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tituskib's avatar

Hashing passwords when registering users

I have a custom register page and a login controller and i want to hash the password, how can i go about? thank you

0 likes
14 replies
nadniesol's avatar

Hi you can use this for hashing

Hash::make($yourPassword)

crnkovic's avatar

$password = Hash::make(request('password'));

You can have Laravel automatically hash the password whenever you set the password attribute, like this:

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

https://laravel.com/docs/5.6/hashing

3 likes
biishmar's avatar

@tituskib

You can also use helper function bcrypt like

$hashedPassword = bcrypt($password);
2 likes
tituskib's avatar

Thank you, problem now is that, where can I place this code public function setPasswordAttribute($value) { $this->attributes['password'] = Hash::make($value); } Register controller or model or where, please

zion's avatar

That function would go in the model.

tituskib's avatar

@crnkovic cannot still hash bro, I am screwed a bit...can you elaborate more. thank you

1 like
zion's avatar

Let's start with you showing what you have thus far and what you have tried.

1 like
rin4ik's avatar

try this

$user->password = Hash::make(request('password'));
$user->save();
1 like
tituskib's avatar

@rin4ik , i think your suggestion is i use the php artisan tinker command. But i need to hash a password entered by a user during registration, I have a custom registration form. Thank you

davidnknight's avatar

You could also use the helper function bcrypt(...) instead of Hash::make(...) if you're using bcrypt.

So your User.php model would look something like this:

<?php

namespace App;

class User extends Authenticatable
{
    ...

    /**
     * Hash the password on save/update.
    */
    public function setPasswordAttribute($value) {
        $this->attributes['password'] = bcrypt($value);
    }
}

In any case, either bcrypt() or Hash::make() within the setPasswordAttribute() method on your User model will result in the password being automatically hashed on save/update.

1 like
cocaengasado's avatar

In my case i added a $cast attribute to the model

protected $casts = [ 'password' = 'hashed' ];

Please or to participate in this conversation.