88use Illuminate \Database \Eloquent \Relations \MorphOne ;
99use Illuminate \Database \Eloquent \Relations \HasMany ;
1010use Illuminate \Database \Eloquent \Relations \MorphMany ;
11- use Illuminate \Database \Eloquent \Builder ;
1211
1312trait HasNestedAttributesTrait
1413{
1514 /**
16- * Defined nested attributes
15+ * Defined nested attributes
1716 *
1817 * @var array
1918 */
@@ -53,12 +52,13 @@ public function fill(array $attributes)
5352 foreach ($ this ->nested as $ attr ) {
5453 if (isset ($ attributes [$ attr ])) {
5554 $ this ->acceptNestedAttributesFor [$ attr ] = $ attributes [$ attr ];
55+ unset($ attributes [$ attr ]);
5656 }
5757 }
5858 }
5959 return parent ::fill ($ attributes );
6060 }
61-
61+
6262 /**
6363 * Save the model to the database.
6464 *
@@ -75,7 +75,7 @@ public function save(array $options = [])
7575
7676 foreach ($ this ->getAcceptNestedAttributesFor () as $ attribute => $ stack ) {
7777 $ methodName = lcfirst (join (array_map ('ucfirst ' , explode ('_ ' , $ attribute ))));
78-
78+
7979 if (!method_exists ($ this , $ methodName )) {
8080 throw new Exception ('The nested atribute relation " ' . $ methodName . '" does not exists. ' );
8181 }
@@ -86,7 +86,7 @@ public function save(array $options = [])
8686 if (!$ this ->saveNestedAttributes ($ relation , $ stack )) {
8787 return false ;
8888 }
89- } else if ($ relation instanceof HasMany || $ relation instanceof MorphMany) {
89+ } elseif ($ relation instanceof HasMany || $ relation instanceof MorphMany) {
9090 foreach ($ stack as $ params ) {
9191 if (!$ this ->saveManyNestedAttributes ($ this ->$ methodName (), $ params )) {
9292 return false ;
@@ -115,7 +115,7 @@ protected function saveNestedAttributes($relation, array $params)
115115 return $ model ->delete ();
116116 }
117117 return $ model ->update ($ stack );
118- } else if ($ relation ->create ($ stack )) {
118+ } elseif ($ relation ->create ($ stack )) {
119119 return true ;
120120 }
121121 return false ;
@@ -135,9 +135,9 @@ protected function saveManyNestedAttributes($relation, array $params)
135135
136136 if ($ this ->allowDestroyNestedAttributes ($ params )) {
137137 return $ model ->delete ();
138- }
138+ }
139139 return $ model ->update ($ params );
140- } else if ($ relation ->create ($ params )) {
140+ } elseif ($ relation ->create ($ params )) {
141141 return true ;
142142 }
143143 return false ;
@@ -153,4 +153,4 @@ protected function allowDestroyNestedAttributes(array $params)
153153 {
154154 return isset ($ params [$ this ->destroyNestedKey ]) && (bool ) $ params [$ this ->destroyNestedKey ] == true ;
155155 }
156- }
156+ }
0 commit comments