Skip to content

Commit 5dd4036

Browse files
authored
Added auditSyncWithPivotValues method (#939)
1 parent c22b1e3 commit 5dd4036

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

src/Auditable.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace OwenIt\Auditing;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
67
use Illuminate\Database\Eloquent\Relations\MorphMany;
78
use Illuminate\Database\Eloquent\SoftDeletes;
89
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Collection;
911
use Illuminate\Support\Facades\App;
1012
use Illuminate\Support\Facades\Config;
1113
use Illuminate\Support\Facades\Event;
@@ -754,7 +756,7 @@ public function auditDetach(string $relationName, $ids = null, $touch = true, $c
754756

755757
/**
756758
* @param string $relationName
757-
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
759+
* @param Collection|Model|array $ids
758760
* @param bool $detaching
759761
* @param array $columns
760762
* @param \Closure|null $callback
@@ -787,7 +789,7 @@ public function auditSync(string $relationName, $ids, $detaching = true, $column
787789

788790
/**
789791
* @param string $relationName
790-
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
792+
* @param Collection|Model|array $ids
791793
* @param array $columns
792794
* @param \Closure|null $callback
793795
* @return array
@@ -800,11 +802,37 @@ public function auditSyncWithoutDetaching(string $relationName, $ids, $columns =
800802
return $this->auditSync($relationName, $ids, false, $columns, $callback);
801803
}
802804

805+
/**
806+
* @param string $relationName
807+
* @param Collection|Model|array $ids
808+
* @param array $values
809+
* @param bool $detaching
810+
* @param array $columns
811+
* @param \Closure|null $callback
812+
* @return array
813+
*/
814+
public function auditSyncWithPivotValues(string $relationName, $ids, array $values, bool $detaching = true, $columns = ['*'], $callback = null)
815+
{
816+
$this->validateRelationshipMethodExistence($relationName, 'syncWithPivotValues');
817+
818+
if ($ids instanceof Model) {
819+
$ids = $ids->getKey();
820+
} elseif ($ids instanceof \Illuminate\Database\Eloquent\Collection) {
821+
$ids = $ids->isEmpty() ? [] : $ids->pluck($ids->first()->getKeyName())->toArray();
822+
} elseif ($ids instanceof Collection) {
823+
$ids = $ids->toArray();
824+
}
825+
826+
return $this->auditSync($relationName, collect(Arr::wrap($ids))->mapWithKeys(function ($id) use ($values) {
827+
return [$id => $values];
828+
}), $detaching, $columns, $callback);
829+
}
830+
803831
/**
804832
* @param string $relationName
805833
* @param string $event
806-
* @param \Illuminate\Support\Collection $old
807-
* @param \Illuminate\Support\Collection $new
834+
* @param Collection $old
835+
* @param Collection $new
808836
* @return void
809837
*/
810838
private function dispatchRelationAuditEvent($relationName, $event, $old, $new)

tests/Functional/AuditingTest.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,49 @@ public function itWillAuditSync()
667667
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
668668
}
669669

670+
/**
671+
* @test
672+
* @return void
673+
*/
674+
public function itWillAuditSyncWithPivotValues()
675+
{
676+
if (version_compare($this->app->version(), '8.0.0', '<')) {
677+
$this->markTestSkipped('This test is only for Laravel 8.0.0+');
678+
}
679+
680+
$firstCategory = factory(Category::class)->create();
681+
$secondCategory = factory(Category::class)->create();
682+
$article = factory(Article::class)->create();
683+
684+
$article->categories()->attach([$firstCategory->getKey() => [ 'pivot_type' => 'PIVOT_1' ]]);
685+
686+
$no_of_audits_before = Audit::where('auditable_type', Article::class)->count();
687+
$categoryBefore = $article->categories()->first()->getKey();
688+
689+
$article->auditSyncWithPivotValues(
690+
'categories',
691+
$secondCategory,
692+
[ 'pivot_type' => 'PIVOT_1' ]
693+
);
694+
695+
$no_of_audits_after = Audit::where('auditable_type', Article::class)->count();
696+
$categoryAfter = $article->categories()->first()->getKey();
697+
698+
$this->assertSame($firstCategory->getKey(), $categoryBefore);
699+
$this->assertSame($secondCategory->getKey(), $categoryAfter);
700+
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
701+
702+
$this->assertSame(
703+
"{$secondCategory->getKey()}",
704+
$article->categories()->pluck('id')->join(',')
705+
);
706+
707+
$this->assertSame(
708+
$secondCategory->getKey(),
709+
$article->categories()->wherePivot('pivot_type', 'PIVOT_1')->first()->getKey()
710+
);
711+
}
712+
670713
/**
671714
* @test
672715
* @return void
@@ -697,7 +740,6 @@ function ($categories) { return $categories->wherePivot('pivot_type', 'PIVOT_1')
697740

698741
$this->assertSame($firstCategory->getKey(), $categoryBefore);
699742
$this->assertSame($secondCategory->getKey(), $categoryAfter);
700-
$this->assertNotSame($categoryBefore, $categoryAfter);
701743
$this->assertGreaterThan($no_of_audits_before, $no_of_audits_after);
702744

703745
$this->assertSame(
@@ -994,12 +1036,12 @@ public function canAuditAnyCustomEvent()
9941036
* @return void
9951037
*/
9961038
public function canAuditCustomAuditModelImplementation()
997-
{
1039+
{
9981040
$audit = null;
9991041
Event::listen(Audited::class, function ($event) use (&$audit) {
10001042
$audit = $event->audit;
10011043
});
1002-
1044+
10031045
$article = new ArticleCustomAuditMorph();
10041046
$article->title = $this->faker->unique()->sentence;
10051047
$article->content = $this->faker->unique()->paragraph(6);

0 commit comments

Comments
 (0)