I'm creating a CMS that allows the user to setup their own nested menus and I'm not sure what way to best handle and save the data i get from the sortable list after serializing to json. I'm not exactly an expect so i figured it'd be a good idea to get some feedback.
My initial idea was to store the menu with a 'menu title', 'item name', an 'order' variable and a 'depth' variable that would denote the nested depth in a table and then using the menu title to group the menu items. Then sort out these variables using a series of for-loops looping through the deserialized json and recursion to find the nested depth, but after giving it some thought this seem to be complicated and somewhat 'messy'.
This is an example of what i get when i dump the deserialized list from jQuery Sortable.
array:1 [
0 => array:3 [
0 => array:2 [
"id" => 1
"children" => array:1 [
0 => array:1 [
0 => array:2 [
"id" => 3
"children" => array:1 [
0 => array:1 [
0 => array:2 [
"id" => 4
"children" => array:1 [
0 => []
]
]
]
]
]
]
]
]
1 => array:2 [
"id" => 2
"children" => array:1 [
0 => array:1 [
0 => array:2 [
"id" => 5
"children" => array:1 [
0 => []
]
]
]
]
]
2 => array:2 [
"id" => 6
"children" => array:1 [
0 => array:2 [
0 => array:2 [
"id" => 8
"children" => array:1 [
0 => []
]
]
1 => array:2 [
"id" => 7
"children" => array:1 [
0 => []
]
]
]
]
]
]
]
This is a pretty straight forward example, but when there's more nested menus etc. five, it seems like it'd require a lot for loops to go through? Am i mistaken in this? There must be a better way? Perhaps changing the structure of the model and making it hierarchical?
Any feedback is appreciated.