betterde/tree

v1.0.8 2020-06-13 02:49 UTC

This package is auto-updated.

Last update: 2024-12-13 13:09:49 UTC


README

use Betterde\Tree\Generator;

$menus = [
    [
        'id' => 1,
        'parent_id' => 0,
        'label' => 'Dashboard',
        'link' => '/dashboard',
        'icon' => null,
        'roles' => 'Admin,'
    ],
    [
        'id' => 2,
        'parent_id' => 1,
        'label' => 'Fiance',
        'link' => '/dashboard/finace',
        'icon' => null,
        'roles' => 'Admin,Fiance'
    ],
    [
        'id' => 3,
        'parent_id' => 1,
        'label' => 'Operation',
        'link' => '/dashboard/operation',
        'icon' => null,
        'roles' => 'Admin,Operation'
    ],
];

$generator = new Generator();
$tree = $generator->make($menus, 'id', 'parent_id', 'sub_menus', 0);

Generated result

[
    [
        'id' => 1,
        'parent_id' => 0,
        'label' => 'Dashboard',
        'link' => '/dashboard',
        'icon' => null,
        'roles' => 'Admin,',
        'sub_menus' => [
            [
                'id' => 2,
                'parent_id' => 1,
                'label' => 'Fiance',
                'link' => '/dashboard/finace',
                'icon' => null,
                'roles' => 'Admin,Fiance'
            ],
            [
                'id' => 3,
                'parent_id' => 1,
                'label' => 'Operation',
                'link' => '/dashboard/operation',
                'icon' => null,
                'roles' => 'Admin,Operation'
            ]
        ]
    ]
];