Menu

1 / 2

I never hash passwords in my controllers — the `hashed` cast does it

DEV Community: laravel·Kamran Khan·4 days ago
#evMRGiMR
#devtolaravel#devto#dev#password#user
Reading 0:00
15s threshold

Every password write in my starter kit flows through one line on the User model:

protected function casts (): array { return [ 'email_verified_at' => 'datetime' , 'password' => 'hashed' , ]; }

Enter fullscreen mode

Exit fullscreen mode

Search my whole backend for Hash::make and you will find nothing. The only Hash call is Hash::check on login, which only reads. Nobody ever writes a hash by hand.

So the register endpoint does this:

$user = User :: create ([ 'name' => $request -> validated ( 'name' ), 'email' => $request -> validated ( 'email' ), 'password' => $request -> validated ( 'password' ), 'role' => 'user' , ]);

Enter fullscreen mode

Exit fullscreen mode

The controller passes the raw password straight through and the model hashes it before it touches the database. Same story in the admin create-user flow, and same in the seeder — DatabaseSeeder passes 'password' => 'password' in plain text and it lands hashed. The seeder does not know or care about bcrypt.

Two things I like about this.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.