jqh 5 years ago
parent
commit
c013183485
2 changed files with 16 additions and 13 deletions
  1. 13 12
      src/Form.php
  2. 3 1
      src/Repositories/EloquentRepository.php

+ 13 - 12
src/Form.php

@@ -877,9 +877,7 @@ class Form implements Renderable
      */
     public function prepareInsert($inserts)
     {
-        if ($this->isHasOneRelation($inserts)) {
-            $inserts = Arr::dot($inserts);
-        }
+        $this->prepareHasOneRelation($inserts);
 
         foreach ($inserts as $column => $value) {
             if (is_null($field = $this->getFieldByColumn($column))) {
@@ -904,21 +902,24 @@ class Form implements Renderable
      *
      * @param array $inserts
      *
-     * @return bool
      */
-    protected function isHasOneRelation($inserts)
+    function prepareHasOneRelation(array &$inserts)
     {
-        $first = current($inserts);
+        $relations = [];
+        $this->builder->fields()->each(function ($field) use (&$relations) {
+            if (Str::contains($field->column(), '.')) {
+                $first = explode('.', $field->column())[0];
 
-        if (!is_array($first)) {
-            return false;
-        }
+                $relations[$first] = null;
+            }
+        });
 
-        if (is_array(current($first))) {
-            return false;
+        foreach ($relations as $first => $v) {
+            if (isset($inserts[$first])) {
+                $inserts = array_merge($inserts, Arr::dot([$first => $inserts[$first]]));
+            }
         }
 
-        return Arr::isAssoc($first);
     }
 
     /**

+ 3 - 1
src/Repositories/EloquentRepository.php

@@ -174,7 +174,9 @@ abstract class EloquentRepository extends Repository
 
             $relations = $this->getRelationInputs($model, $updates);
 
-            $updates = Arr::except($updates, array_keys($relations));
+            if ($relations) {
+                $updates = Arr::except($updates, array_keys($relations));
+            }
 
             foreach ($updates as $column => $value) {
                 $model->setAttribute($column, $value);