Skip to content

Commit b666417

Browse files
committed
handle has one dot notation
1 parent 9c34be7 commit b666417

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/app/Library/Uploaders/Support/Interfaces/UploaderInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function relationship(bool $isRelation): self;
3535
*/
3636
public function getName(): string;
3737

38+
public function getAttributeName(): string;
39+
3840
public function getDisk(): string;
3941

4042
public function getPath(): string;

src/app/Library/Uploaders/Support/RegisterUploadEvents.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ private function registerEvents(array|null $subfield = [], ?bool $registerModelE
4646
$attributes = $this->crudObject->getAttributes();
4747
$model = $attributes['model'] ?? get_class($this->crudObject->crud()->getModel());
4848
$uploader = $this->getUploader($attributes, $this->uploaderConfiguration);
49+
50+
if (isset($attributes['relation_type']) && $attributes['entity'] !== false) {
51+
$uploader = $uploader->relationship(true);
52+
}
4953

5054
$this->setupModelEvents($model, $uploader);
5155
$this->setupUploadConfigsInCrudObject($uploader);

src/app/Library/Uploaders/Uploader.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ public function storeUploadedFiles(Model $entry): Model
7474
if ($this->attachedToFakeField) {
7575
$fakeFieldValue = $entry->{$this->attachedToFakeField};
7676
$fakeFieldValue = is_string($fakeFieldValue) ? json_decode($fakeFieldValue, true) : (array) $fakeFieldValue;
77-
$fakeFieldValue[$this->getName()] = $this->uploadFiles($entry);
77+
$fakeFieldValue[$this->getAttributeName()] = $this->uploadFiles($entry);
7878

7979
$entry->{$this->attachedToFakeField} = isset($entry->getCasts()[$this->attachedToFakeField]) ? $fakeFieldValue : json_encode($fakeFieldValue);
8080

8181
return $entry;
8282
}
8383

84-
$entry->{$this->getName()} = $this->uploadFiles($entry);
84+
$entry->{$this->getAttributeName()} = $this->uploadFiles($entry);
8585

8686
return $entry;
8787
}
@@ -118,6 +118,11 @@ public function getName(): string
118118
return $this->name;
119119
}
120120

121+
public function getAttributeName(): string
122+
{
123+
return Str::afterLast($this->name, '.');
124+
}
125+
121126
public function getDisk(): string
122127
{
123128
return $this->disk;
@@ -200,11 +205,11 @@ public function uploadFiles(Model $entry, $values = null)
200205

201206
private function retrieveFiles(Model $entry): Model
202207
{
203-
$value = $entry->{$this->name};
208+
$value = $entry->{$this->getAttributeName()};
204209

205210
if ($this->handleMultipleFiles) {
206-
if (! isset($entry->getCasts()[$this->name]) && is_string($value)) {
207-
$entry->{$this->name} = json_decode($value, true);
211+
if (! isset($entry->getCasts()[$this->getName()]) && is_string($value)) {
212+
$entry->{$this->getAttributeName()} = json_decode($value, true);
208213
}
209214

210215
return $entry;
@@ -214,13 +219,13 @@ private function retrieveFiles(Model $entry): Model
214219
$values = $entry->{$this->attachedToFakeField};
215220
$values = is_string($values) ? json_decode($values, true) : (array) $values;
216221

217-
$values[$this->name] = isset($values[$this->name]) ? Str::after($values[$this->name], $this->path) : null;
222+
$values[$this->getAttributeName()] = isset($values[$this->getAttributeName()]) ? Str::after($values[$this->getAttributeName()], $this->path) : null;
218223
$entry->{$this->attachedToFakeField} = json_encode($values);
219224

220225
return $entry;
221226
}
222227

223-
$entry->{$this->name} = Str::after($value, $this->path);
228+
$entry->{$this->getAttributeName()} = Str::after($value, $this->path);
224229

225230
return $entry;
226231
}

0 commit comments

Comments
 (0)