diff --git a/src/BelongsToMany.php b/src/BelongsToMany.php index 46f8f1d..a14bbe9 100644 --- a/src/BelongsToMany.php +++ b/src/BelongsToMany.php @@ -28,6 +28,8 @@ public function attach($id, array $attributes = [], $touch = true) $this->parent->firePivotAttachedEvent(); + $this->parent->resetPivotChanges(); + return $result; } @@ -60,6 +62,8 @@ public function detach($ids = null, $touch = true) $this->parent->firePivotDetachedEvent(); + $this->parent->resetPivotChanges(); + return $result; } @@ -85,6 +89,8 @@ public function updateExistingPivot($id, array $attributes, $touch = true) $this->parent->firePivotUpdatedEvent(); + $this->parent->resetPivotChanges(); + return $result; } } diff --git a/src/HasPivotEvents.php b/src/HasPivotEvents.php index c7c38a4..ccd8a86 100644 --- a/src/HasPivotEvents.php +++ b/src/HasPivotEvents.php @@ -2,6 +2,7 @@ namespace Signifly\PivotEvents; +use Illuminate\Support\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; @@ -9,14 +10,14 @@ trait HasPivotEvents { protected $pivotChanges = []; - public function setPivotChanges(string $type, string $relation, array $ids = []) + public function setPivotChanges(string $type, string $relation, array $ids = []): void { collect($ids)->each(function ($attributes, $id) use ($type, $relation) { data_set($this->pivotChanges, "{$type}.{$relation}.{$id}", $attributes); }); } - public function getPivotChanges($type = null) + public function getPivotChanges($type = null): Collection { if ($type) { return collect(data_get($this->pivotChanges, $type)); @@ -25,11 +26,16 @@ public function getPivotChanges($type = null) return collect($this->pivotChanges); } - public function getPivotChangeIds($type, $relation) + public function getPivotChangeIds($type, $relation): Collection { return collect($this->getPivotChanges("{$type}.{$relation}"))->keys(); } + public function resetPivotChanges(): void + { + $this->pivotChanges = []; + } + public static function pivotAttaching($callback) { static::registerModelEvent('pivotAttaching', $callback); @@ -98,15 +104,12 @@ public function firePivotUpdatedEvent($halt = false) public function getObservableEvents() { return array_merge( + parent::getObservableEvents(), [ - 'retrieved', 'creating', 'created', 'updating', 'updated', - 'saving', 'saved', 'restoring', 'restored', - 'deleting', 'deleted', 'forceDeleted', 'pivotAttaching', 'pivotAttached', 'pivotDetaching', 'pivotDetached', 'pivotUpdating', 'pivotUpdated', - ], - $this->observables + ] ); } @@ -123,9 +126,16 @@ public function getObservableEvents() * @param string $relationName * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ - protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, - $parentKey, $relatedKey, $relationName = null) - { + protected function newBelongsToMany( + Builder $query, + Model $parent, + $table, + $foreignPivotKey, + $relatedPivotKey, + $parentKey, + $relatedKey, + $relationName = null + ) { return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); } }