@@ -86,62 +86,68 @@ public function update(Request $request)
8686 return $ response ;
8787 }
8888
89- $ tableName = $ request ->table ;
90- $ originalColumns = Table::getColumnsName ($ tableName );
91- $ columns = json_decode ($ request ->columns );
92- $ fields = json_decode ($ request ->fields );
89+ $ tableName = $ request ->table ;
90+ $ columns = json_decode ($ request ->columns );
91+ $ fields = json_decode ($ request ->fields );
9392
9493 $ errors = $ this ->validation ($ fields , $ columns , 'update ' );
9594
9695 if (count ($ errors ) > 0 ) {
9796 return $ this ->generateError ($ errors );
9897 }
9998
100- $ object = DBM ::Object ()->where ('name ' , $ tableName )->first ();
101- $ model = $ object ->model ;
102- $ details = $ object ->details ;
103- $ key = $ details ['findColumn ' ];
99+ $ object = DBM ::Object ()->where ('name ' , $ tableName )->first ();
104100
105- if (!class_exists ($ model )) {
101+ if (!class_exists ($ object -> model )) {
106102 return $ this ->generateError (["Model not found. Please create model first " ]);
107103 }
108104
109105 try {
106+ $ this ->updateData ($ request , $ object );
107+ return response ()->json (['success ' => true ]);
108+ } catch (\Exception $ e ) {
109+ return $ this ->generateError ([$ e ->getMessage ()]);
110+ }
111+ }
110112
111- $ table = DBM ::model ($ model , $ tableName )->where ($ key , $ columns ->{$ key })->first ();
113+ return response ()->json (['success ' => false ]);
114+ }
112115
113- foreach ($ columns as $ column => $ value ) {
116+ public function updateData ($ request , $ object )
117+ {
118+ $ tableName = $ request ->table ;
119+ $ originalColumns = Table::getColumnsName ($ tableName );
120+ $ columns = json_decode ($ request ->columns );
121+ $ fields = json_decode ($ request ->fields );
122+ $ key = $ object ->details ['findColumn ' ];
114123
115- if ( in_array ( $ column , $ originalColumns )) {
124+ $ table = DBM :: model ( $ object -> model , $ tableName )-> where ( $ key , $ columns ->{ $ key })-> first ();
116125
117- if ($ request ->hasFile ($ column )) {
118- $ value = $ this ->saveFiles ($ request , $ column , $ tableName );
119- }
126+ foreach ($ columns as $ column => $ value ) {
120127
121- if ($ value !== null && $ value !== "" ) {
128+ if (in_array ( $ column , $ originalColumns ) ) {
122129
123- if (!Driver::isMongoDB ()) {
124- if ($ functionName = $ this ->hasFunction ($ fields , $ column )) {
125- $ value = $ this ->executeFunction ($ functionName , $ value );
126- }
127- }
130+ if ($ request ->hasFile ($ column )) {
131+ $ value = $ this ->saveFiles ($ request , $ column , $ tableName );
132+ }
128133
129- $ table ->{$ column } = $ this ->prepareStoreField ($ value , $ tableName , $ column );
134+ if ($ value !== null && $ value !== "" ) {
135+
136+ if (!Driver::isMongoDB ()) {
137+ if ($ functionName = $ this ->hasFunction ($ fields , $ column )) {
138+ $ value = $ this ->executeFunction ($ functionName , $ value );
130139 }
131140 }
132- }
133141
134- if ($ table ->update ()) {
135- $ this ->updateRelationshipData ($ fields , $ columns , $ object , $ table );
136- return response ()->json (['success ' => true ]);
142+ $ table ->{$ column } = $ this ->prepareStoreField ($ value , $ tableName , $ column );
137143 }
138-
139- } catch (\Exception $ e ) {
140- return $ this ->generateError ([$ e ->getMessage ()]);
141144 }
142145 }
143146
144- return response ()->json (['success ' => false ]);
147+ if ($ table ->update ()) {
148+ $ this ->updateRelationshipData ($ fields , $ columns , $ object , $ table );
149+ return response ()->json (['success ' => true ]);
150+ }
145151 }
146152
147153 public function delete (Request $ request )
0 commit comments