使用Laravel中的数据库创建父子管理员菜单树

时间:2018-12-05 07:55:25

标签: php laravel menu

我正在开发内容管理系统,但是CMS中菜单的子级-父级关系遇到了问题。

基本上,我有一个可以创建菜单和子菜单的系统。这是数据库中的数组。

 array:6 [
       0 => array:4 [
        "id" => 4
        "name" => "Contacts"
        "order" => 0
        "parent_menu_id" => null
      ]
      1 => array:4 [
        "id" => 1
        "name" => "Leads"
        "order" => 1
        "parent_menu_id" => null
      ]
      2 => array:4 [
        "id" => 2
        "name" => "List Leads"
        "slug" => "list-leads"
        "order" => 1
        "parent_menu_id" => 1
      ]
      3 => array:4 [
        "id" => 5
        "name" => "Edit Leads"
        "slug" => "edit-leads"
        "order" => 1
        "parent_menu_id" => 1
      ]
      4 => array:4 [
        "id" => 3
        "name" => "Create New"
        "slug" => "new"
        "order" => 2
        "parent_menu_id" => 2
      ]
      5 => array:4 [
        "id" => 14
        "name" => "Tasks"
        "slug" => "tasks"
        "order" => 3
        "parent_menu_id" => null
      ]
    ]

我正在使用菜单表中的父ID插入菜单和子菜单。我如上所述从数据库中获取菜单。那么,我将如何以树状结构等分层形式生成/呈现菜单?

我需要遍历一个可以进行任意数量导航的数组,并将其智能地排序到其父子关系中。我能够做到,但是只有一个级别的深度。它需要管理带有无限多个层的带有多个子项的子项,并将其输出到HTML下拉菜单和下拉项嵌套列表中。

我正在创建一个函数,它仅显示父母和孩子,而不显示任何孩子的孩子。我该如何解决?

    public static function render_module_menu() {
          $menus = MenusModel::leftJoin('modules', 'menus.module_id', '=', 'modules.id')->where('modules.enable', 1)->select('menus.*')->orderBy('order')->get()->toArray();

          foreach ($menus as $menu) {
            $childMenus = MenusModel::where('parent_menu_id', $menu['id'])->orderBy('order')->get()->toArray();

            if ($childMenus) {
              echo '<li class="nav-item dropdown">
                      <a class="nav-link dropdown-toggle" href="#" id="pagesDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <i class="'.$menu['icon'].'"></i>
                        <span>'.$menu['name'].'</span>
                      </a>
                      <div class="dropdown-menu" aria-labelledby="pagesDropdown">';
              foreach ($childMenus as $child) {
                 echo '<a class="dropdown-item" href="'.url($menu['slug'].'/'.$child['slug']).'">
                          <i class="'.$child['icon'].'" style="margin-right: 10px;"></i>
                       '.$child['name'].'</a>';

              }
              echo '</div>
                    </li>';
            } else {
                echo '<li class="nav-item">
                        <a class="nav-link" href="'.url($menu['slug']).'">
                          <i class="'.$menu['icon'].'"></i>
                          <span>'.$menu['name'].'</span></a>
                      </li>';
            }

          }
        }

2 个答案:

答案 0 :(得分:2)

第一个功能:

public static function GenerateMenuArray($arr, $parent = 0)
{
$pages = Array();
foreach($arr as $page)
{
if($page['parent_menu_id'] == $parent)
{
$page['sub'] = isset($page['sub']) ? $page['sub'] : self::GenerateMenuArray($arr, $page['id']);
$pages[] = $page;
}
}
return $pages;
}

第二个功能:

// loop the multidimensional array recursively to generate the HTML
public static function GenerateMenuHTML($nav, $level=0)
{
$menus = MenusModel::leftJoin('modules', 'menus.module_id', '=', 'modules.id')->select('menus.*')->where('modules.enable', 1)->where('parent_menu_id', null)->orderBy('order')->get()->toArray();

$html = '';
foreach($nav as $page)
{
// dd($page);
if (empty($page['sub'])) {
if ($page['parent_menu_id'] == null) {
$html .= '<li class="nav-item">
<a class="nav-link" href="'.url($page['slug']).'">
<i class="'.$page['icon'].'"></i>
<span>'.$page['name'].'</span></a>
</li>';
} else {
foreach ($menus as $parent) {
$parent_slug = $parent['slug'];
if($parent['id'] == $page['parent_menu_id'] AND $page['external'] == 0) {
$html .= '<a class="dropdown-item" href="'.url($parent_slug.'/'.$page['slug']).'">
<i class="'.$page['icon'].'" style="margin-right: 10px;"></i>
'.$page['name'].'</a>';
}
}

if ($page['external'] == 1) {
$html .= '<a class="dropdown-item" href="'.url($page['slug']).'">
<i class="'.$page['icon'].'" style="margin-right: 10px;"></i>
'.$page['name'].'</a>';
}

}

} else {
if ($level > 0) {
$html .= '</div></li>';
}
$html .= '<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="pagesDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="'.$page['icon'].'"></i>
<span>'.$page['name'].'</span></a>
<div class="dropdown-menu" aria-labelledby="pagesDropdown">';


$level = ++$level;
$html .= self::GenerateMenuHTML($page['sub'], $level);
$level = --$level;

if ($level > 0) {
$html .= '</div></li>';
}
}
}
return $html;
}

// loop the multidimensional array recursively to generate the HTML
public static function GenerateNavHTML($nav)
{
$html = '';
foreach($nav as $page)
{
$html .= '<ul><li>';
$html .= '<a href="' . $page['slug'] . '">' . $page['name'] . '</a>';
$html .= self::GenerateNavHTML($page['sub']);
$html .= '</li></ul>';
}
return $html;
}

请尝试这个。

答案 1 :(得分:0)

您只循环浏览菜单及其直接子菜单。
最好的解决方案是创建一个打印子项的函数,然后在其内部调用该函数,以便只要当前菜单项中有子项,它将一直被调用。

<div>