33namespace  Eloquent \NestedAttributes \Traits ;
44
55use  Exception ;
6- use  Illuminate \Support \Facades \DB ;
7- use  Illuminate \Database \Eloquent \Relations \HasOne ;
8- use  Illuminate \Database \Eloquent \Relations \MorphOne ;
6+ use  Illuminate \Database \Eloquent \Relations ;
97use  Illuminate \Database \Eloquent \Relations \HasMany ;
8+ use  Illuminate \Database \Eloquent \Relations \HasOne ;
109use  Illuminate \Database \Eloquent \Relations \MorphMany ;
10+ use  Illuminate \Database \Eloquent \Relations \MorphOne ;
11+ use  Illuminate \Support \Facades \DB ;
1112
1213trait  HasNestedAttributesTrait
1314{
1415    /** 
15-      * Defined nested attributes 
16+      * Defined nested attributes.  
1617     * 
1718     * @var array 
1819     */ 
1920    protected  $ acceptNestedAttributesFor
2021
2122    /** 
22-      * Defined "destroy" key name 
23+      * Defined "destroy" key name.  
2324     * 
2425     * @var string 
2526     */ 
2627    protected  $ destroyNestedKey'_destroy ' ;
2728
28- 
2929    /** 
30-      * Get accept nested attributes 
30+      * Get accept nested attributes.  
3131     * 
3232     * @return array 
3333     */ 
34-     public  function  getAcceptNestedAttributesFor ()
34+     public  function  getAcceptNestedAttributesFor ():  array 
3535    {
3636        return  $ this acceptNestedAttributesFor ;
3737    }
@@ -44,9 +44,9 @@ public function getAcceptNestedAttributesFor()
4444     * 
4545     * @throws \Illuminate\Database\Eloquent\MassAssignmentException 
4646     */ 
47-     public  function  fill (array  $ attributes
47+     public  function  fill (array  $ attributes:  self 
4848    {
49-         if  (!empty ($ this nested )) {
49+         if  (!  empty ($ this nested )) {
5050            $ this acceptNestedAttributesFor  = [];
5151
5252            foreach  ($ this nested  as  $ attr
@@ -56,6 +56,7 @@ public function fill(array $attributes)
5656                }
5757            }
5858        }
59+ 
5960        return  parent ::fill ($ attributes
6061    }
6162
@@ -65,30 +66,30 @@ public function fill(array $attributes)
6566     * @param  array  $options 
6667     * @return bool 
6768     */ 
68-     public  function  save (array  $ options
69+     public  function  save (array  $ options:  bool 
6970    {
7071        DB ::beginTransaction ();
7172
72-         if  (!parent ::save ($ options
73+         if  (!  parent ::save ($ options
7374            return  false ;
7475        }
7576
7677        foreach  ($ this getAcceptNestedAttributesFor () as  $ attribute$ stack
77-             $ methodNamelcfirst (join (array_map ('ucfirst ' , explode ('_ ' , $ attribute
78+             $ methodNamelcfirst (implode (array_map ('ucfirst ' , explode ('_ ' , $ attribute
7879
79-             if  (!method_exists ($ this $ methodName
80+             if  (!  method_exists ($ this $ methodName
8081                throw  new  Exception ('The nested atribute relation " '  . $ methodName'" does not exists. ' );
8182            }
8283
8384            $ relation$ this $ methodName
8485
8586            if  ($ relationinstanceof  HasOne || $ relationinstanceof  MorphOne) {
86-                 if  (!$ this saveNestedAttributes ($ relation$ stack
87+                 if  (!  $ this saveNestedAttributes ($ relation$ stack
8788                    return  false ;
8889                }
8990            } elseif  ($ relationinstanceof  HasMany || $ relationinstanceof  MorphMany) {
9091                foreach  ($ stackas  $ params
91-                     if  (!$ this saveManyNestedAttributes ($ this $ methodName$ params
92+                     if  (!  $ this saveManyNestedAttributes ($ this $ methodName$ params
9293                        return  false ;
9394                    }
9495                }
@@ -98,6 +99,7 @@ public function save(array $options = [])
9899        }
99100
100101        DB ::commit ();
102+ 
101103        return  true ;
102104    }
103105
@@ -108,16 +110,18 @@ public function save(array $options = [])
108110     * @param  array                                   $params 
109111     * @return bool 
110112     */ 
111-     protected  function  saveNestedAttributes ($ relationarray  $ params
113+     protected  function  saveNestedAttributes (Relations   $ relationarray  $ params:  bool 
112114    {
113115        if  ($ this exists  && $ model$ relationfirst ()) {
114116            if  ($ this allowDestroyNestedAttributes ($ params
115117                return  $ modeldelete ();
116118            }
119+ 
117120            return  $ modelupdate ($ stack
118121        } elseif  ($ relationcreate ($ stack
119122            return  true ;
120123        }
124+ 
121125        return  false ;
122126    }
123127
@@ -128,28 +132,30 @@ protected function saveNestedAttributes($relation, array $params)
128132     * @param  array                                   $params 
129133     * @return bool 
130134     */ 
131-     protected  function  saveManyNestedAttributes ($ relationarray  $ params
135+     protected  function  saveManyNestedAttributes ($ relationarray  $ params:  bool 
132136    {
133137        if  (isset ($ params'id ' ]) && $ this exists ) {
134138            $ model$ relationfindOrFail ($ params'id ' ]);
135139
136140            if  ($ this allowDestroyNestedAttributes ($ params
137141                return  $ modeldelete ();
138142            }
143+ 
139144            return  $ modelupdate ($ params
140145        } elseif  ($ relationcreate ($ params
141146            return  true ;
142147        }
148+ 
143149        return  false ;
144150    }
145151
146152    /** 
147-      * Check can we delete nested data 
153+      * Check can we delete nested data.  
148154     * 
149155     * @param  array $params 
150156     * @return bool 
151157     */ 
152-     protected  function  allowDestroyNestedAttributes (array  $ params
158+     protected  function  allowDestroyNestedAttributes (array  $ params:  bool 
153159    {
154160        return  isset ($ params$ this destroyNestedKey ]) && (bool ) $ params$ this destroyNestedKey ] == true ;
155161    }
0 commit comments