Home Laravel: Check with Observer if Column was Changed on Update
Reply: 1

Laravel: Check with Observer if Column was Changed on Update

Adam
1#
Adam Published in 2018-02-14 17:43:38Z

I am using an Observer 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()) ?
    }

}
Adam
2#
Adam Reply to 2018-02-14 18:39:11Z

As tadman already said in the comments, the method isDirty does the trick:

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::find($user->id)->email; 
      }
    }

}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.352053 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO