There are model events: https://laravel.com/docs/9.x/eloquent#events
Sep 9, 2022
4
Level 4
Modify model date on update or create event
Hi all,
I am currently updating an app and I am looking for a way to ensure all user emails are stored as lowercase while the first_name and last_name to ucwords which would allow me to have a standard format throughout my application data.
Is there any boot method or middleware to help me do that instead of looking for every use of model create/update methods to modify them?
thank you, Mihail
Level 102
@tudosm Exactly yes. And reassign the email :)
$user->email = strtolower($user->email);
For instance
protected static function booted()
{
static::updating(function ($user) {
$user->email = strtolower($user->email);
});
}
1 like
Please or to participate in this conversation.