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

allow selecting columns to be saved when using auditDetach(), auditSync() and auditSyncWithoutDetaching() #850

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Changes from all 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
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);
}
}