Laravel's Model observer class is not working when updating using query builder

My model class is User and observer class is UserObserver.

I have added some code in the updated method of UserObserver that will run everytime the User model update function is used.

The code in the UserObserver works when User data update like shown below:
User::find(2)->update(['name'=>'Update Name']);
However, the code in the UserObserver won't run when User data is updated in the following way:
User::where('id', 2)->update(['name'=>'Update Name']);
When I debug I can understand that User::find(2) return User model object and User::where('id', 2) will return a Builder

So, how can I make use of our observer class method regardless of whether it is updated using User Model object or Builder object?

Asked: February 22, 2016 by administrator
Posted under: programming