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',
];
}
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',
]);
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. First, hashing is a decision that can never be forgotten. There is no new controller where someone (me, three months from now) forgets to hash before saving. The wrong thing is impossible. Second, the cast does not double-hash: if the value is already hashed it leaves it alone, so the factory's pre-hashed 'password' stays exactly as it is.
The login side never touches storage:
if (! $user || ! Hash::check($request->validated('password'), $user->password)) {
return response()->json(['message' => 'Invalid credentials'], 401);
}
Check on the way in, cast on the way out. One job each.
I have stopped treating password hashing as something each controller does and started treating it as something the model guarantees. That is the whole pattern.
Patterns like this one — auth, roles, admin CRUD, dashboard — are what my Laravel + React SaaS starter kit is made of, so every project starts from them instead of rediscovering them: https://kamranofficial.gumroad.com/l/mhekoig
Top comments (7)
Good pattern, and I've run it in production too. The one failure mode I'd flag: the cast hashes blindly, so if a seed, migration backfill, or CSV import ever writes an already-hashed value, you get a hash of a hash and a user who can never log in. My rule now is that the cast only ever receives raw values, and any bulk or import path gets verified with Hash::check afterward. Keeps the elegance, removes the footgun.
Fair flag, Syed, and I've been bitten by this exact shape of bug on a CSV import where the export already contained bcrypt hashes. One nuance worth adding: the cast itself skips values that are already hashed, so going through the model you're covered. The real footgun is the paths that skip the model entirely, backfills with DB::table, raw SQL, an import writing straight to the table. That's where I now normalize everything to plain text at the edge and spot check a few rows with Hash::check after the run. Same rule as yours, just enforced one step earlier.
Great addition, Kamran — that's the more precise way to frame it. The model path is covered, so the real discipline is making sure every write path actually goes through the model, and treating any raw-table write as suspicious by default. Normalizing to plain text at the edge plus a Hash::check spot-check after the run is a solid habit — I'm stealing that one. Thanks for sharing the war story!
Exactly, that is the discipline that matters most in practice. I have started treating any write that skips the model as a review flag in itself, not just a style choice, because that is where a plain text password slips through unnoticed. The spot check after an import is what gives me confidence the edge conversion actually happened, rather than assuming the cast did its job. Thanks for pushing the framing to write paths, that wording is clearer than how I put it in the post.
Glad the framing landed, Kamran. Auditing by write path is the move: it catches raw-table writes on their own instead of relying on style rules. Good to hear the review-flag habit is already paying off in your reviews, and thanks for coming back with the update.
That is how I am using it now. In review I treat any write that skips the model as a question first, not a style note, because that is the one that decides whether the cast ever runs. The spot check is still the part that keeps me honest after an import.
Exactly the right instinct. The cast only protects you if the model is the one doing the write, so any path that skips it is the real failure, not a style detail. Spot checks after imports are the honest part of the whole thing. I do the same now: a quick scan after any bulk import to make sure nothing landed raw.