Skip to content

Commit 496b81c

Browse files
authored
Merge pull request #5233 from Laravel-Backpack/fix-upload-return-path-in-repeatable-uploaders
Fix upload return path in repeatable uploaders
2 parents 4f334df + c494dd1 commit 496b81c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/app/Library/Uploaders/Support/Traits/HandleRepeatableUploads.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Support\Collection;
99
use Illuminate\Support\Facades\Storage;
10+
use Illuminate\Support\Str;
1011

1112
trait HandleRepeatableUploads
1213
{
@@ -91,6 +92,20 @@ private function retrieveRepeatableFiles(Model $entry): Model
9192
return $this->retrieveFiles($entry);
9293
}
9394

95+
$repeatableUploaders = app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName());
96+
97+
$values = $entry->{$this->getRepeatableContainerName()};
98+
$values = is_string($values) ? json_decode($values, true) : $values;
99+
$values = array_map(function ($item) use ($repeatableUploaders) {
100+
foreach ($repeatableUploaders as $upload) {
101+
$item[$upload->getName()] = $this->getValuesWithPathStripped($item, $upload);
102+
}
103+
104+
return $item;
105+
}, $values);
106+
107+
$entry->{$this->getRepeatableContainerName()} = $values;
108+
94109
return $entry;
95110
}
96111

@@ -172,4 +187,16 @@ private function getPreviousRepeatableValues(Model $entry, UploaderInterface $up
172187

173188
return $previousValues ?? [];
174189
}
190+
191+
private function getValuesWithPathStripped(array|string|null $item, UploaderInterface $upload)
192+
{
193+
$uploadedValues = $item[$upload->getName()] ?? null;
194+
if (is_array($uploadedValues)) {
195+
return array_map(function ($value) use ($upload) {
196+
return Str::after($value, $upload->getPath());
197+
}, $uploadedValues);
198+
}
199+
200+
return isset($uploadedValues) ? Str::after($uploadedValues, $upload->getPath()) : null;
201+
}
175202
}

0 commit comments

Comments
 (0)