Laravel: Check with Observer if Column was Changed on Update
You don't have to get the user again from the database. The following should work:
public function updating(User $user)
{
if($user->isDirty('email')){
// email has changed
$new_email = $user->email;
$old_email = $user->getOriginal('email');
}
}
Edit: Credits to https://stackoverflow.com/a/54307753/2311074 for getOriginal
As tadman
already said in the comments, the method isDirty
does the trick:
class UserObserver
{
/**
* Listen to the User updating event.
*
* @param \App\User $user
* @return void
*/
public function updating(User $user)
{
if($user->isDirty('email')){
// email has changed
$new_email = $user->email;
$old_email = $user->getOriginal('email');
}
}
}
If you want to know the difference between isDirty
and wasChanged
, see https://stackoverflow.com/a/49350664/2311074