In this episode, we dive into how you can automatically hash user passwords in Laravel using mutators, making your code a lot cleaner and safer. We start by looking at the old way—explicitly calling Hash::make or other helpers when creating or updating a user—which, while straightforward, can clutter your code and makes it easy to forget to hash passwords.
You'll see quick examples of creating simple user creation and update routes, then we refactor things step by step. We move from manual array building and merging, to using Laravel’s handy only
method for streamlined data handling. Then, the real magic happens: we implement a mutator (a special setter function) on the User model. This ensures any time the password attribute is set, it’s automatically hashed, no matter where it comes from—making it almost impossible to accidentally store plain passwords.
We also cover some edge cases, like making sure empty passwords don’t overwrite existing ones with blanks. This makes your updates much safer and prevents those “oh no, I lost my password” moments for your users.
By the end of the video, you’ll see how much cleaner and DRY this mutation approach makes your code. Plus, this method gives you a blueprint for handling similar situations with other sensitive attributes or models in your Laravel apps.