Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
Reset pivot changes after dispatching events
Browse files Browse the repository at this point in the history
  • Loading branch information
pactode committed Aug 5, 2019
1 parent f325ea8 commit f35c3aa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function attach($id, array $attributes = [], $touch = true)

$this->parent->firePivotAttachedEvent();

$this->parent->resetPivotChanges();

return $result;
}

Expand Down Expand Up @@ -60,6 +62,8 @@ public function detach($ids = null, $touch = true)

$this->parent->firePivotDetachedEvent();

$this->parent->resetPivotChanges();

return $result;
}

Expand All @@ -85,6 +89,8 @@ public function updateExistingPivot($id, array $attributes, $touch = true)

$this->parent->firePivotUpdatedEvent();

$this->parent->resetPivotChanges();

return $result;
}
}
32 changes: 21 additions & 11 deletions src/HasPivotEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

namespace Signifly\PivotEvents;

use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

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));
Expand All @@ -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);
Expand Down Expand Up @@ -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
]
);
}

Expand All @@ -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);
}
}

0 comments on commit f35c3aa

Please sign in to comment.