@@ -19,6 +19,8 @@ public function updateTreeOrder($request)
1919 {
2020 $ primaryKey = $ this ->model ->getKeyName ();
2121
22+ $ columns = $ this ->getOperationSetting ('reorderColumnNames ' );
23+
2224 // we use the upsert method that should update the values of the matching ids.
2325 // it has the drawback of creating new entries when the id is not found
2426 // for that reason we get a list of all the ids and filter the ones
@@ -28,14 +30,29 @@ public function updateTreeOrder($request)
2830 // filter the items that are not in the database and map the request
2931 $ reorderItems = collect ($ request )->filter (function ($ item ) use ($ itemKeys ) {
3032 return $ item ['item_id ' ] !== '' && $ item ['item_id ' ] !== null && $ itemKeys ->contains ($ item ['item_id ' ]);
31- })->map (function ($ item ) use ($ primaryKey ) {
33+ })->map (function ($ item ) use ($ primaryKey, $ columns ) {
3234 $ item [$ primaryKey ] = $ item ['item_id ' ];
33- $ item ['parent_id ' ] = empty ($ item ['parent_id ' ]) ? null : $ item ['parent_id ' ];
34- $ item ['depth ' ] = empty ($ item ['depth ' ]) ? null : (int ) $ item ['depth ' ];
35- $ item ['lft ' ] = empty ($ item ['left ' ]) ? null : (int ) $ item ['left ' ];
36- $ item ['rgt ' ] = empty ($ item ['right ' ]) ? null : (int ) $ item ['right ' ];
35+ $ item [$ columns ['parent_id ' ]] = empty ($ item ['parent_id ' ]) ? null : $ item ['parent_id ' ];
36+ $ item [$ columns ['depth ' ]] = empty ($ item ['depth ' ]) ? null : (int ) $ item ['depth ' ];
37+ $ item [$ columns ['lft ' ]] = empty ($ item ['left ' ]) ? null : (int ) $ item ['left ' ];
38+ $ item [$ columns ['rgt ' ]] = empty ($ item ['right ' ]) ? null : (int ) $ item ['right ' ];
39+
3740 // unset mapped items properties.
38- unset($ item ['item_id ' ], $ item ['left ' ], $ item ['right ' ]);
41+ if ($ columns ['parent_id ' ] !== 'parent_id ' ) {
42+ unset($ item ['parent_id ' ]);
43+ }
44+ if ($ columns ['depth ' ] !== 'depth ' ) {
45+ unset($ item ['depth ' ]);
46+ }
47+ if ($ columns ['lft ' ] !== 'left ' ) {
48+ unset($ item ['left ' ]);
49+ }
50+ if ($ columns ['rgt ' ] !== 'right ' ) {
51+ unset($ item ['right ' ]);
52+ }
53+
54+ // unset the item_id property
55+ unset($ item ['item_id ' ]);
3956
4057 return $ item ;
4158 })->toArray ();
@@ -47,13 +64,13 @@ public function updateTreeOrder($request)
4764 });
4865
4966 // wrap the queries in a transaction to avoid partial updates
50- DB ::connection ($ this ->model ->getConnectionName ())->transaction (function () use ($ reorderItems , $ primaryKey , $ itemKeys ) {
67+ DB ::connection ($ this ->model ->getConnectionName ())->transaction (function () use ($ reorderItems , $ primaryKey , $ itemKeys, $ columns ) {
5168 // create a string of ?,?,?,? to use as bind placeholders for item keys
5269 $ reorderItemsBindString = implode (', ' , array_fill (0 , count ($ reorderItems ), '? ' ));
5370
5471 // each of this properties will be updated using a single query with a CASE statement
5572 // this ensures that only 4 queries are run, no matter how many items are reordered
56- foreach ([ ' parent_id ' , ' depth ' , ' lft ' , ' rgt ' ] as $ column ) {
73+ foreach (array_values ( $ columns ) as $ column ) {
5774 $ query = '' ;
5875 $ bindings = [];
5976 $ query .= "UPDATE {$ this ->model ->getTable ()} SET {$ column } = CASE " ;
0 commit comments