8
8
use Illuminate \Database \Eloquent \Relations \MorphOne ;
9
9
use Illuminate \Database \Eloquent \Relations \HasMany ;
10
10
use Illuminate \Database \Eloquent \Relations \MorphMany ;
11
- use Illuminate \Database \Eloquent \Builder ;
12
11
13
12
trait HasNestedAttributesTrait
14
13
{
15
14
/**
16
- * Defined nested attributes
15
+ * Defined nested attributes
17
16
*
18
17
* @var array
19
18
*/
@@ -53,12 +52,13 @@ public function fill(array $attributes)
53
52
foreach ($ this ->nested as $ attr ) {
54
53
if (isset ($ attributes [$ attr ])) {
55
54
$ this ->acceptNestedAttributesFor [$ attr ] = $ attributes [$ attr ];
55
+ unset($ attributes [$ attr ]);
56
56
}
57
57
}
58
58
}
59
59
return parent ::fill ($ attributes );
60
60
}
61
-
61
+
62
62
/**
63
63
* Save the model to the database.
64
64
*
@@ -75,7 +75,7 @@ public function save(array $options = [])
75
75
76
76
foreach ($ this ->getAcceptNestedAttributesFor () as $ attribute => $ stack ) {
77
77
$ methodName = lcfirst (join (array_map ('ucfirst ' , explode ('_ ' , $ attribute ))));
78
-
78
+
79
79
if (!method_exists ($ this , $ methodName )) {
80
80
throw new Exception ('The nested atribute relation " ' . $ methodName . '" does not exists. ' );
81
81
}
@@ -86,7 +86,7 @@ public function save(array $options = [])
86
86
if (!$ this ->saveNestedAttributes ($ relation , $ stack )) {
87
87
return false ;
88
88
}
89
- } else if ($ relation instanceof HasMany || $ relation instanceof MorphMany) {
89
+ } elseif ($ relation instanceof HasMany || $ relation instanceof MorphMany) {
90
90
foreach ($ stack as $ params ) {
91
91
if (!$ this ->saveManyNestedAttributes ($ this ->$ methodName (), $ params )) {
92
92
return false ;
@@ -115,7 +115,7 @@ protected function saveNestedAttributes($relation, array $params)
115
115
return $ model ->delete ();
116
116
}
117
117
return $ model ->update ($ stack );
118
- } else if ($ relation ->create ($ stack )) {
118
+ } elseif ($ relation ->create ($ stack )) {
119
119
return true ;
120
120
}
121
121
return false ;
@@ -135,9 +135,9 @@ protected function saveManyNestedAttributes($relation, array $params)
135
135
136
136
if ($ this ->allowDestroyNestedAttributes ($ params )) {
137
137
return $ model ->delete ();
138
- }
138
+ }
139
139
return $ model ->update ($ params );
140
- } else if ($ relation ->create ($ params )) {
140
+ } elseif ($ relation ->create ($ params )) {
141
141
return true ;
142
142
}
143
143
return false ;
@@ -153,4 +153,4 @@ protected function allowDestroyNestedAttributes(array $params)
153
153
{
154
154
return isset ($ params [$ this ->destroyNestedKey ]) && (bool ) $ params [$ this ->destroyNestedKey ] == true ;
155
155
}
156
- }
156
+ }
0 commit comments