Popular JSON dynamically with PHP / Laravel

1

I'm working with Laravel on a client project where I want to build a tree structure with the users registered in the system, I found a script that does this, and for that it uses JSONs to get the data.

The framework it works on 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 did not understand the logic it will generate from auto-forming. 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 you let this user know that "id_user_parent":null first and the rest 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 from 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 do I format the structure that comes from the response from my database to the structure expected to generate the users tree?

    
asked by anonymous 01.10.2018 / 20:07

2 answers

1

So, if this loads a user here:

$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 this, and more this:

$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 build the structure you want

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

    
01.10.2018 / 20:27
0

I do not know if it's the right way but I followed your idea and put the following structure together:

$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 feedback at the moment is this:

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, exactly like the waiting, the problem is that it is taking position 1 when it enters the foreach to verify that it is a child node. And I have to start with position 0. How do I change positions?

    
01.10.2018 / 21:50