0

I'm creating a laravel-react application and trying to create a Treeview that needs to be populated from the MySql database.

This is my sample Treeview populating code that is working and shows my Treeview.

<TreeView
              data={[
                  {
                      id: 0,
                      label: 'Father A',
                      children: [
                          {
                              id: 1,
                              label: 'Child 1'
                          },
                          {
                              id: 2,
                              label: 'Child 2'
                          }
                      ],
                  },
                  {
                      id: 3,
                      label: 'Father 2',
                      children: [
                          {
                              id: 4,
                              label:'Child 3',
                              children: [
                                  {
                                      id: 5,
                                      label: 'Child 1 of Child 3',
                                  }
                              ],
                              
                          },
                          {
                              id:6,
                              label:'Child 4',
                              children:[
                                  {
                                      id:7,
                                      label:'Child 5 of Child 4'
                                  }
                              ]
                          }
                      ]
                  }
              ]}
              renderNode={({ label }) => <div>{label}</div>}
              />

Now I want to generate the same type of JSON object from the table with the below fields;

table fields: id, parent_id, label

I have managed to get the following type of output as the response JSON object from the table using console.log(JSON.stringify(response));

{"data":[
{"id":1,"parent_id":0,"label":"ROOT","created_at":null,"updated_at":null},
{"id":2,"parent_id":1,"label":"Father 1","created_at":null,"updated_at":null},
{"id":3,"parent_id":1,"label":"Father 2","created_at":null,"updated_at":null},...

What is the best way to convert this output JSON to a similar format to my sample JSON used to populate the Treeview? or is there a better way to achieve the desired output using Treeview?

Any help would be highly appreciated.

Ruwan Liyanage
  • 333
  • 1
  • 13
  • Please share you database schema as well, and did you make self relation ? please also share your relations – Abdul Haseeb Khan Sep 17 '22 at 20:52
  • Just 3 fields I'm using > id, parent_id, and label, and I already have the JSON objects list returned from the DB. I just want to format the existing JSON list to replace parent_id with a collection of children so I can have a tree of elements that matches the data required by Treeview. Is there any better built-in method to do this without looping through the objects list? Thanks – Ruwan Liyanage Sep 19 '22 at 08:30
  • 1
    I have answered a question similar of yours may this help you https://stackoverflow.com/questions/71777535/laravel-how-can-i-reduce-duplicate-codes/71816394#71816394 – Abdul Haseeb Khan Sep 19 '22 at 11:12

0 Answers0