Skip to content

Commit

Permalink
Merge pull request #850 from s4muel/master
Browse files Browse the repository at this point in the history
allow selecting columns to be saved when using auditDetach(), auditSync() and auditSyncWithoutDetaching()
MortenDHansen authored Aug 25, 2023
2 parents 5b217a7 + dcb1a6b commit 7d929c0
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Auditable.php
Original file line number Diff line number Diff line change
@@ -675,7 +675,7 @@ public function auditAttach(string $relationName, $id, array $attributes = [], $
* @return int
* @throws AuditingException
*/
public function auditDetach(string $relationName, $ids = null, $touch = true)
public function auditDetach(string $relationName, $ids = null, $touch = true, $columns = ['*'])
{
if (!method_exists($this, $relationName) || !method_exists($this->{$relationName}(), 'detach')) {
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method detach');
@@ -684,11 +684,11 @@ public function auditDetach(string $relationName, $ids = null, $touch = true)
$this->auditEvent = 'detach';
$this->isCustomEvent = true;
$this->auditCustomOld = [
$relationName => $this->{$relationName}()->get()->toArray()
$relationName => $this->{$relationName}()->get($columns)->toArray()
];
$results = $this->{$relationName}()->detach($ids, $touch);
$this->auditCustomNew = [
$relationName => $this->{$relationName}()->get()->toArray()
$relationName => $this->{$relationName}()->get($columns)->toArray()
];
Event::dispatch(AuditCustom::class, [$this]);
$this->isCustomEvent = false;
@@ -703,7 +703,7 @@ public function auditDetach(string $relationName, $ids = null, $touch = true)
* @return array
* @throws AuditingException
*/
public function auditSync($relationName, $ids, $detaching = true)
public function auditSync($relationName, $ids, $detaching = true, $columns = ['*'])
{
if (!method_exists($this, $relationName) || !method_exists($this->{$relationName}(), 'sync')) {
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method sync');
@@ -712,7 +712,7 @@ public function auditSync($relationName, $ids, $detaching = true)
$this->auditEvent = 'sync';

$this->auditCustomOld = [
$relationName => $this->{$relationName}()->get()->toArray()
$relationName => $this->{$relationName}()->get($columns)->toArray()
];

$changes = $this->{$relationName}()->sync($ids, $detaching);
@@ -722,7 +722,7 @@ public function auditSync($relationName, $ids, $detaching = true)
$this->auditCustomNew = [];
} else {
$this->auditCustomNew = [
$relationName => $this->{$relationName}()->get()->toArray()
$relationName => $this->{$relationName}()->get($columns)->toArray()
];
}

@@ -740,11 +740,11 @@ public function auditSync($relationName, $ids, $detaching = true)
* @return array
* @throws AuditingException
*/
public function auditSyncWithoutDetaching(string $relationName, $ids)
public function auditSyncWithoutDetaching(string $relationName, $ids, $columns = ['*'])
{
if (!method_exists($this, $relationName) || !method_exists($this->{$relationName}(), 'syncWithoutDetaching')) {
throw new AuditingException('Relationship ' . $relationName . ' was not found or does not support method syncWithoutDetaching');
}
return $this->auditSync($relationName, $ids, false);
return $this->auditSync($relationName, $ids, false, $columns);
}
}

0 comments on commit 7d929c0

Please sign in to comment.