Skip to content

Commit

Permalink
allow selecting columns to be saved when using auditDetach(), auditSy…
Browse files Browse the repository at this point in the history
…nc() and auditSyncWithoutDetaching()
  • Loading branch information
s4muel committed Aug 24, 2023
1 parent 5b217a7 commit dcb1a6b
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
Expand Up @@ -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');
Expand All @@ -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;
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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()
];
}

Expand All @@ -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 dcb1a6b

Please sign in to comment.