From a508c6a3bd4dfc2978425266ffb097309baa5af3 Mon Sep 17 00:00:00 2001 From: Pringgo Radianto Date: Thu, 10 Jun 2021 23:25:23 +0700 Subject: [PATCH 1/2] Fix image and fIle field --- src/Helpers/FieldHelper.php | 43 +++++++++++++++++++++++++++++ src/Observers/BelongsToObserver.php | 6 ++-- src/Observers/HasManyObserver.php | 10 +++++-- src/Observers/HasOneObserver.php | 9 ++++-- 4 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 src/Helpers/FieldHelper.php diff --git a/src/Helpers/FieldHelper.php b/src/Helpers/FieldHelper.php new file mode 100644 index 0000000..296e6db --- /dev/null +++ b/src/Helpers/FieldHelper.php @@ -0,0 +1,43 @@ +map(function ($value) { + if ($value instanceof UploadedFile) + return $value->hashName(); + + return $value; + }) + ->toArray(); + } + + /** + * Generate bulk field + * + * @param array $fields + * + * @return array + */ + public static function generateMany(array $fields): array + { + return collect($fields) + ->map(function ($field) { + return self::generate($field); + }) + ->toArray(); + } +} diff --git a/src/Observers/BelongsToObserver.php b/src/Observers/BelongsToObserver.php index 3f58e71..b53136b 100644 --- a/src/Observers/BelongsToObserver.php +++ b/src/Observers/BelongsToObserver.php @@ -3,6 +3,7 @@ namespace KirschbaumDevelopment\NovaInlineRelationship\Observers; use Illuminate\Database\Eloquent\Model; +use KirschbaumDevelopment\NovaInlineRelationship\Helpers\FieldHelper; class BelongsToObserver extends BaseObserver { @@ -18,7 +19,7 @@ public function updating(Model $model, $attribute, $value) } if (count($value)) { - $parentModel->update($value[0]['fields']); + $parentModel->update(FieldHelper::generate($value[0]['fields'])); } } @@ -28,7 +29,8 @@ public function updating(Model $model, $attribute, $value) public function creating(Model $model, $attribute, $value) { if (count($value)) { - $parentModel = $model->{$attribute}()->getRelated()->newInstance($value[0]['fields']); + $field = FieldHelper::generate($value[0]['fields']); + $parentModel = $model->{$attribute}()->getRelated()->newInstance($field); $parentModel->save(); $model->{$attribute}()->associate($parentModel); } diff --git a/src/Observers/HasManyObserver.php b/src/Observers/HasManyObserver.php index 25c4e4a..e054b93 100644 --- a/src/Observers/HasManyObserver.php +++ b/src/Observers/HasManyObserver.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use Illuminate\Database\Eloquent\Model; +use KirschbaumDevelopment\NovaInlineRelationship\Helpers\FieldHelper; class HasManyObserver extends BaseObserver { @@ -21,13 +22,15 @@ public function updating(Model $model, $attribute, $value) for ($i = 0; $i < count($value); $i++) { $childModel = $model->{$attribute}()->find($value[$i]['modelId']); + $field = FieldHelper::generate($value[$i]['fields']); + if (empty($childModel)) { - $model->{$attribute}()->create($value[$i]['fields']); + $model->{$attribute}()->create($field); continue; } - $childModel->update($value[$i]['fields']); + $childModel->update($field); } } @@ -36,6 +39,7 @@ public function updating(Model $model, $attribute, $value) */ public function created(Model $model, $attribute, $value) { - $model->{$attribute}()->createMany(Arr::pluck($value, 'fields')); + $fields = FieldHelper::generateMany(Arr::pluck($value, 'fields')); + $model->{$attribute}()->createMany($fields); } } diff --git a/src/Observers/HasOneObserver.php b/src/Observers/HasOneObserver.php index 576f024..f66bb2e 100644 --- a/src/Observers/HasOneObserver.php +++ b/src/Observers/HasOneObserver.php @@ -3,6 +3,7 @@ namespace KirschbaumDevelopment\NovaInlineRelationship\Observers; use Illuminate\Database\Eloquent\Model; +use KirschbaumDevelopment\NovaInlineRelationship\Helpers\FieldHelper; class HasOneObserver extends BaseObserver { @@ -12,13 +13,14 @@ class HasOneObserver extends BaseObserver public function updating(Model $model, $attribute, $value) { $childModel = $model->{$attribute}()->first(); + $field = FieldHelper::generate($value[0]['fields']); if (! empty($childModel)) { count($value) - ? $childModel->update($value[0]['fields']) + ? $childModel->update($field) : $childModel->delete(); } elseif (count($value)) { - $model->{$attribute}()->create($value[0]['fields']); + $model->{$attribute}()->create($field); } } @@ -28,7 +30,8 @@ public function updating(Model $model, $attribute, $value) public function created(Model $model, $attribute, $value) { if (count($value)) { - $model->{$attribute}()->create($value[0]['fields']); + $field = FieldHelper::generate($value[0]['fields']); + $model->{$attribute}()->create($field); } } } From 95270b023f9d8795a7983b19a521953a7062bd80 Mon Sep 17 00:00:00 2001 From: Pringgo Radianto Date: Fri, 11 Jun 2021 06:56:59 +0700 Subject: [PATCH 2/2] Replace collection with native array map --- src/Helpers/FieldHelper.php | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Helpers/FieldHelper.php b/src/Helpers/FieldHelper.php index 296e6db..8d2fbd2 100644 --- a/src/Helpers/FieldHelper.php +++ b/src/Helpers/FieldHelper.php @@ -8,36 +8,32 @@ class FieldHelper { /** * Generate field - * + * * @param array $field - * - * @return array + * + * @return array */ public static function generate(array $field): array { - return collect($field) - ->map(function ($value) { - if ($value instanceof UploadedFile) - return $value->hashName(); + return array_map(function ($value) { + if ($value instanceof UploadedFile) + return $value->hashName(); - return $value; - }) - ->toArray(); + return $value; + }, $field); } /** * Generate bulk field - * + * * @param array $fields - * - * @return array + * + * @return array */ public static function generateMany(array $fields): array { - return collect($fields) - ->map(function ($field) { - return self::generate($field); - }) - ->toArray(); + return array_map(function ($field) { + return self::generate($field); + }, $fields); } }