Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My workaround to rebuildTree(). #575

Open
userpta005 opened this issue Feb 12, 2023 · 0 comments
Open

My workaround to rebuildTree(). #575

userpta005 opened this issue Feb 12, 2023 · 0 comments

Comments

@userpta005
Copy link

userpta005 commented Feb 12, 2023

This solution updates existing nodes and also creates new nodes if they are added. But it doesn't remove the old nodes.

$array = [
            [
                'name' => 'home',
                'description' => 'Painel'
            ],
            [
                'name' => 'management_group_view',
                'description' => 'Gerenciamento',
                'children' => [
                    [
                        'name' => 'users_group_view',
                        'description' => 'Usuários',
                        'children' => [
                            array('name' => 'users_view', 'description' => 'Visualizar'),
                            array('name' => 'users_create', 'description' => 'Criar'),
                            array('name' => 'users_edit', 'description' => 'Editar'),
                            array('name' => 'users_delete', 'description' => 'Deletar'),
                        ]
                    ],
                    [
                        'name' => 'roles_group_view',
                        'description' => 'Atribuições',
                        'children' => [
                            array('name' => 'roles_view', 'description' => 'Visualizar'),
                            array('name' => 'roles_create', 'description' => 'Criar'),
                            array('name' => 'roles_edit', 'description' => 'Editar'),
                            array('name' => 'roles_delete', 'description' => 'Deletar'),
                        ]
                    ],
                    [
                        'name' => 'permissions_group_view',
                        'description' => 'Permissões',
                        'children' => [
                            array('name' => 'permissions_view', 'description' => 'Visualizar'),
                            array('name' => 'permissions_create', 'description' => 'Criar'),
                            array('name' => 'permissions_edit', 'description' => 'Editar'),
                            array('name' => 'permissions_delete', 'description' => 'Deletar')
                        ]
                    ]
                ]
            ]
        ];

        function percorrerArray($array, $parent = null)
            {
                foreach ($array as $value) {
                    if (array_key_exists('name', $value)) {
                        $permission = Permission::updateOrCreate(
                            ['name' => $value['name']],
                            [
                                'description' => $value['description'],
                                'parent_id' => $parent ? $parent->id : null,
                            ]
                        );

                        if (array_key_exists('children', $value)) {
                            percorrerArray($value['children'], $permission);
                        }
                    }
                }
            }

            percorrerArray($array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant