Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for auditSync and auditDetach on Auditable custom Pivot class #954

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand All @@ -13,6 +14,7 @@
use Illuminate\Support\Facades\Event;
use OwenIt\Auditing\Contracts\AttributeEncoder;
use OwenIt\Auditing\Contracts\AttributeRedactor;
use OwenIt\Auditing\Contracts\Auditable as ContractsAuditable;
use OwenIt\Auditing\Contracts\Resolver;
use OwenIt\Auditing\Events\AuditCustom;
use OwenIt\Auditing\Exceptions\AuditableTransitionException;
Expand Down Expand Up @@ -746,7 +748,14 @@ public function auditDetach(string $relationName, $ids = null, $touch = true, $c
}

$old = $relationCall->get($columns);
$results = $relationCall->detach($ids, $touch);
$pivotClass = $relationCall->getPivotClass();
if($pivotClass !== Pivot::class && is_a($pivotClass,ContractsAuditable::class,true)){
$results = $pivotClass::withoutAuditing(function () use ($relationCall, $ids, $touch) {
return $relationCall->detach($ids, $touch);
});
}else{
$results = $relationCall->detach($ids, $touch);
}
$new = $relationCall->get($columns);

$this->dispatchRelationAuditEvent($relationName, 'detach', $old, $new);
Expand All @@ -772,9 +781,16 @@ public function auditSync(string $relationName, $ids, $detaching = true, $column
if ($callback instanceof \Closure) {
$this->applyClosureToRelationship($relationCall, $callback);
}

$old = $relationCall->get($columns);
willpower232 marked this conversation as resolved.
Show resolved Hide resolved
$changes = $relationCall->sync($ids, $detaching);
$pivotClass = $relationCall->getPivotClass();
if($pivotClass !== Pivot::class && is_a($pivotClass,ContractsAuditable::class,true)){
$changes =$pivotClass::withoutAuditing(function () use ($relationCall, $ids, $detaching) {
return $relationCall->sync($ids, $detaching);
});
}else{
$changes = $relationCall->sync($ids, $detaching);
}

if (collect($changes)->flatten()->isEmpty()) {
$old = $new = collect([]);
Expand Down