Skip to content

Commit

Permalink
fix: deleteAttachment method
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmeira committed Sep 26, 2023
1 parent e267fb5 commit ef9fe46
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Traits/HasAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use GSMeira\LaravelAttachments\Enums\AttachmentsAppend;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

trait HasAttachments
{
Expand Down Expand Up @@ -85,21 +84,23 @@ protected function attachments(): Attribute

public function deleteAttachment(string|array $value): void
{
$rawAttachments = $this->getRawAttachments();
$attachments = $this->getRawAttachments();

$backupAttachments = [];

if (is_array($value)) {
$backupAttachments = array_map(fn ($key) => [ $key => $rawAttachments[$key] ], $value);
foreach ($value as $key) {
$backupAttachments[$key] = $attachments[$key];
}
} else {
$backupAttachments[$value] = $rawAttachments[$value];
$backupAttachments[$value] = $attachments[$value];
}

foreach ($backupAttachments as $key => $path) {
unset($rawAttachments[$key]);
unset($attachments[$key]);
}

$this->update(compact('rawAttachments'));
$this->update(compact('attachments'));

$fs = $this->getAttachmentsFs();

Expand Down

0 comments on commit ef9fe46

Please sign in to comment.