A little late to the party, but might be helpful for future devs seeking a similar solution.
I recommend using the package Laravel Attribute Observer, as an alternative to polluting your Service Providers or filling your Observers with isDirty()
and wasChanged()
boilerplate.
So your use case would look like this:
class UserObserver
{
/**
* Handle changes to the "email" field of User on "updating" events.
*
* @param \App\User $user
* @param string $newValue The current value of the field
* @param string $oldValue The previous value of the field
* @return void
*/
public function onEmailUpdating(User $user, string $newValue, string $oldValue)
{
// Your logic goes here...
}
}
Laravel Attribute Observer is especially useful when you have a lot of attributes to observe on one or more models.
Disclaimer: I am the author of Laravel Attribute Observer. If it saves you some development time, consider buying me a coffee.