Dynamically popular JSON with PHP/Laravel

Asked

Viewed 272 times

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?

2 answers

1

So if this here loads a user:

$users = User::select('name_first', 'name_second', 'id_user_parent')->get()

you need to create an object, and popular it according to its structure.

For example:

$data['nome'] = "julio"

your controller’s response will be

{"nome":"julio"}

if you do, and more:

$data['users'] = $users

the final answer will be

{
 "nome":"julio",
 "users":[
    {
      "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
    }
 ]
}

and so it goes, until you assemble the structure you want

To load the data? You can use the available querys in the eloquent of the Laravel, or create querys to assemble your complete object.

0

I do not know if it is the most correct way but I followed your idea and I put together the following structure:

$users = User::select('name_first', 'name_second', 'id_user_parent')->get();

foreach ($users as $user) {
    if ($user['id_user_parent'] == null) {
        $main['name'] = $user['name_first'] . ' ' . $user['name_second'];
        $main['parent'] = $user['id_user_parent'];

        $data = $main;
    }
}
foreach($users as $key => $user) {
    if ($user['id_user_parent'] != null) {
        $data['children'][$key]['name'] = $user['name_first'] . ' ' . $user['name_second'];
        $data['children'][$key]['parent'] = $user['id_user_parent'];
    }
}
dd($data);

return response()->json($data);

And my return at the moment is as follows:

array:3 [▼
    "name" => "Tiago Revers Paza"
    "parent" => null
    "children" => array:2 [▼
        1 => array:2 [▼
            "name" => "Yuri Luiz Hugo da Cunha"
            "parent" => 1
        ]
        2 => array:2 [▼
            "name" => "Severino Benício das Neves"
            "parent" => 1
        ]
    ]
]

That is, as long as waiting for him, the problem is that he is taking position 1 when he enters the foreach to check if it is a child node. And I have to start with position 0. How do I change positions?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.