laravel-pivot icon indicating copy to clipboard operation
laravel-pivot copied to clipboard

Duplicate custom listener registered when using with laravel-pivot

Open lunfel opened this issue 2 years ago • 0 comments

For a project of mine, I created a custom model event on a model. This model uses the PivotEventTrait. The custom event is added to the model's $observables array. As we can see in the code below, the $observables from the model are merged twice. Once from laravel-pivot and then when calling the parent in Laravel Framework.

This cause listeners being registered twice for a single event type.

The fix I would suggest would be to remove $observables from the merge in laravel-pivot. I don't know though if there are any other impacts to fixing this way.

// From Laravel Framework
public function getObservableEvents()
    {
        return array_merge(
            [
                'retrieved', 'creating', 'created', 'updating', 'updated',
                'saving', 'saved', 'restoring', 'restored', 'replicating',
                'deleting', 'deleted', 'forceDeleted',
            ],
            $this->observables
        );
    }
// From laravel-pivot
public function getObservableEvents()
    {
        return array_merge(
            parent::getObservableEvents(),
            [
                'pivotAttaching', 'pivotAttached',
                'pivotDetaching', 'pivotDetached',
                'pivotUpdating', 'pivotUpdated',
            ],
            $this->observables
        );
    }

lunfel avatar Jul 16 '21 21:07 lunfel