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

Commit

Permalink
Merge branch 'master' of github.com:signifly/laravel-pivot-events
Browse files Browse the repository at this point in the history
  • Loading branch information
pactode committed Sep 9, 2019
2 parents 3d2493b + 2d5f0c1 commit ee889e5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
composer.lock
.phpunit.result.cache
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);
}
}
25 changes: 25 additions & 0 deletions tests/PivotEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ public function it_dispatches_pivot_update_events()
Event::assertDispatched('eloquent.pivotUpdated: '.User::class);
}

/** @test */
public function it_resets_pivot_changes_after_an_event_has_been_dispatched()
{
$user = $this->createUser();
$roleA = $this->createRole(['name' => 'role-a']);
$roleB = $this->createRole(['name' => 'role-b']);

$calledTimes = 0;

User::pivotAttached(function ($model) use (&$calledTimes) {
$this->assertCount(1, $model->getPivotChanges());
$calledTimes++;
});

$user->roles()->attach($roleA->id);
$this->assertCount(0, $user->getPivotChanges());

$user->roles()->attach($roleB->id);
$this->assertCount(0, $user->getPivotChanges());

$this->assertCount(2, $user->roles);

$this->assertEquals(2, $calledTimes);
}

protected function createRole(array $overwrites = [])
{
return Role::create(array_merge([
Expand Down

0 comments on commit ee889e5

Please sign in to comment.