Laravel multiple listeners for a model event
<p>I have a trait called RecordsUserActivity which basically create a record in an activities table when a User creates, updates or deletes something that uses this trait. Here is the code:</p>

<pre><code>trait RecordsUserActivity
{
    protected static function boot()
    {
        parent::boot();
        foreach (static::getModelEvents() as $event) {
            static::$event(function ($model) use ($event) {
                $model-&gt;addActivity($event);
            });
        }
    }

    protected function addActivity($event)
    {
        $newActivity = [
            'subject_id' =&gt; $this-&gt;id,
            'subject_type' =&gt; get_class($this),
            'action' =&gt; $event,
            'user_id' =&gt; (Auth::id()) ?? null,
        ];

        if ($event == 'updated') {
            $newActivity['data'] = json_encode($this-&gt;getDirty());
        }

        UserActivity::create($newActivity);
    }

    protected static function getModelEvents()
    {
        if (isset(static::$recordEvents)) {
            return static::$recordEvents;
        }

        return ['created', 'deleted', 'updated'];
    }
}
</code></pre>

<p>Then I have another trait which records property changes in models that use it. Here is the code:</p>

<pre><code>trait RecordsPropertyChangelog
{
    protected static function boot()
    {
        parent::boot();

        static::updated(function ($model){
            $model-&gt;addPropertiesChangelog();
        });
    }

    protected function addPropertiesChangelog()
    {
        $dirty = $this-&gt;getDirty();
        foreach ($dirty as $field =&gt; $newData) {
            $oldData = $this-&gt;getOriginal($field);
            $this-&gt;addPropertyChangelog($field,$oldData,$newData);
        }
    }

    protected function addPropertyChangelog($fieldName,$oldValue,$newValue)
    {
        PropertyChangelog::create([
            'resource_id' =&gt; $this-&gt;id,
            'resource_type' =&gt; get_class($this),
            'property' =&gt; $fieldName,
            'from_value' =&gt; $oldValue,
            'to_value' =&gt; $newValue,
            'data' =&gt; '{}',
        ]);
    }

}
</code></pre>

<p>The problem appears when I include both traits in a model and an update is made, there is some kind of collision with both updated model events. Is there some way to fix this or should I find out another solution?</p>