ahmadbadpey
1 year ago
274
8
Laravel

make a hierarchical structure for instances of a model in laravel

Posted 1 year ago by ahmadbadpey

In the User model I added a column named supervisor that can hold another user_id for each user.

In fact each User can have a supervisor user that specified by that column.

For that I added a hierarchical method like this :

    protected function hierarchical($users)
    {
        $users = $users->toArray();

        $nestedKeys = array();

        foreach ($users as $key => $node) {
            $supervisor_id = $node['supervisor']['user_id'];
            $index         = array_search($supervisor_id, array_column($users, 'user_id'));

            if (!is_null($supervisor_id) && $index != -1) {

                $users[$index]['children'][] = $node;

                $nestedKeys[] = $node['user_id'];
            }
        }

        return $users;
    }

That method accepts a $users that is a collection comes from User::all() for example, that created a response like this But the result is not what I want:

[
    {
        "user_id": 1,
        "supervisor": null,
        "name": "ahmad",
        "family": "badpey",
        "full_name": "ahmad badpey",
        "children": [
            {
                "user_id": 7,
                "supervisor": {
                    "user_id": 1,
                    "name": "ahmad",
                    "family": "badpey",
                    "full_name": "ahmad badpey"
                },
                "name": "ali",
                "family": "nasiri arani",
                "full_name": "ali nasiri arani"
            },
            {
                "user_id": 8,
                "supervisor": {
                    "user_id": 1,
                    "name": "ahmad",
                    "family": "badpey",
                    "full_name": "ahmad badpey"
                },
                "name": "مصطفی",
                "family": "زاهدی",
                "full_name": "مصطفی زاهدی"
            }
        ]
    },
    {
        "user_id": 7,
        "supervisor": {
            "user_id": 1,
            "name": "ahmad",
            "family": "badpey",
            "full_name": "ahmad badpey"
        },
        "name": "ali",
        "family": "nasiri arani",
        "full_name": "ali nasiri arani"
    },
    {
        "user_id": 8,
        "supervisor": {
            "user_id": 1,
            "name": "ahmad",
            "family": "badpey",
            "full_name": "ahmad badpey"
        },
        "name": "مصطفی",
        "family": "زاهدی",
        "full_name": "مصطفی زاهدی",
        "children": [
            {
                "user_id": 9,
                "supervisor": {
                    "user_id": 8,
                    "name": "مصطفی",
                    "family": "زاهدی",
                    "full_name": "مصطفی زاهدی"
                },
                "name": "حسین",
                "family": "قناتی",
                "full_name": "حسین قناتی"
            }
        ]
    },
    {
        "user_id": 9,
        "supervisor": {
            "user_id": 8,
            "name": "مصطفی",
            "family": "زاهدی",
            "full_name": "مصطفی زاهدی"
        },
        "name": "حسین",
        "family": "قناتی",
        "full_name": "حسین قناتی"
    }
]

As you can see created structure is not correct because subordinate users comes Separate again.

What is the best approch to create what I want for model with similar structure?

Please sign in or create an account to participate in this conversation.