Laravel: Model Events when no changes in Provider

Viewed 342

When updating user model ( user::update() ) it triggers "Model Events":

  1. User::updating
  2. User::updated

Only if there're some value changes, Is there a way to trigger another "Event" if there's no changes? thanks

//Provider

public function boot()
{
    User::updated(function ($user) {
        //do something
    });
}
1 Answers

You could check if the model has changed using the isDirty() method, and fire an event manually.. for example:

User::updating(function ($user) {
    if ( ! $user->isDirty())
    {
        event(...);
    }    
});

Example of isDirty()

$user = App\User::first();
$user->isDirty();          //false
$user->name = "Test";
$user->isDirty();          //true

but do this before updated I guess you should be using the updating event here.

Related