laravel observer check if field changed 1

laravel observer check if field changed

class UserObserver
{


    /**
     * Listen to the User created 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');
      }
    }

}

Here is what the above code is Doing:
1. We are listening to the User model’s updating event.
2. We are checking if the email field has changed.
3. If the email field has changed, we are storing the new email in the $new_email variable and the old email in the $old_email variable.

Now, let’s send an email to the user with the new email address.

Similar Posts