Automatically Format Laravel Database Fields with Accessors and Mutators
There’s a really neat feature in Laravel that often gets overlooked because of the feature’s complicated sounding name. We’re talking about accessors and mutators.
What exactly are accessors and mutators?
Accessors: Format something when retrieving from the database Mutators: Format something when saving to the database Both of these can help us save immense amounts of time (and frustration) because we know that whenever retrieving or saving information to our database, we will know it will always be saved in the correct format.
One of the most common uses for a mutator is to be sure that a user’s password is always hashed before it gets saved into the database. This ensures that you don’t have to always remember to hash a password wherever you save a user.
This post is submitted by one of our members. You may submit a new post here.