1
I am working with Laravel in a project of a client where I intend to assemble a tree structure with registered users on the system, I found a script that performs this, and for this he uses Jsons to obtain the data.
The structure that it works is as follows:
{
"name": "Top Level",
"parent": null,
"children": [{
"name": "Level 2: A",
"parent": "Top Level",
"children": [{
"name": "Son of A",
"parent": "Level 2: A"
},
{
"name": "Daughter of A",
"parent": "Level 2: A"
}
]
},
{
"name": "Level 2: B",
"parent": "Top Level"
}
]
}
I tried to prepare my JSON in the same format, but I didn’t understand the logic it will generate of automatic form. I have a user in my database that as in the example it has "parent": null
in my case: "id_user_parent":null
, and all other users are related to other user Ids.
My question is, how do I let this user "id_user_parent":null
at first and the others he popular with their respective children?
My call in my controller:
$users = User::select('name_first', 'name_second', 'id_user_parent')->get()
And the answer JSON of my bank is this:
[
{
"name_first": "Tiago",
"name_second": "Revers Paza",
"id_user_parent": null
},
{
"name_first": "Yuri",
"name_second": "Luiz Hugo da Cunha",
"id_user_parent": 1
},
{
"name_first": "Severino",
"name_second": "Ben\u00edcio das Neves",
"id_user_parent": 1
}
]
That is, how I format the structure that comes from my database response to the structure expected to generate the user tree?