Question: I am using an [Observer](https://laravel.com/docs/5.5/eloquent#observers) to watch if a user was updated.

Whenever a user is `updated` I would like to check if his `email` has been changed. 

Is something like this possible?

```
class UserObserver
{

    /**
     * Listen to the User created event.
     *
     * @param  \App\User  $user
     * @return void
     */
    public function updating(User $user)
    {
      // if($user->hasChangedEmailInThisUpdate()) ?
    }

}

```

Answer: 
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](https://github.com/alexstewartja/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:

```php
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](https://github.com/alexstewartja/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](https://github.com/alexstewartja/laravel-attribute-observer)**. If it saves you some development time, consider buying me a coffee.*
Instead of using the `updating` event, You should use the `updated` model event to compare the column changes against the original. Please have a look at the below code.

AppServiceProvider.php

```
<?php

namespace App\Providers;

use App\Models\User;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        User::updated(function($user){
            $changes = array_diff($user->getOriginal(), $user->getAttributes());
            if(array_key_exists('email',$changes)){
                /* do your things. */
            }
        });
    }
}

```

Explanation:

* Once you get the difference then you need to use `array_key_exists` to check whether the email field got updated or not.