jeudi 9 février 2017

Avoid Eloquent model event from being triggered multiple/ infinite times using Laravel

I have the following code:

AppServiceProvider:

User::saved(function (User $user) {
    return $user->modifiedEvent();
});

User::deleted(function (User $user)   
    return $user->modifiedEvent();
});

In my User model I have:

    public function modifiedEvent()
    {
        $stage = $this->isDirty('terms_and_conditions') 
             && $this->terms_and_conditions !== null ? 'completed' : 'incomplete';
        $this->stage_terms_and_conditions = $stage;
        return $this->save();
    }

The issue I have is that the modifiedEvent also modifies the model, which means we get an infinite loop!

Whats the best way to make sure that this event is not triggered multiple times due caused by the event it self.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire