diff --git a/src/Helpers/FieldHelper.php b/src/Helpers/FieldHelper.php new file mode 100644 index 0000000..8d2fbd2 --- /dev/null +++ b/src/Helpers/FieldHelper.php @@ -0,0 +1,39 @@ +hashName(); + + return $value; + }, $field); + } + + /** + * Generate bulk field + * + * @param array $fields + * + * @return array + */ + public static function generateMany(array $fields): array + { + return array_map(function ($field) { + return self::generate($field); + }, $fields); + } +} 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); } } }