Blogger Information
Blog 64
fans 6
comment 2
visits 83034
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel--通用后台管理系统--从数据库中自动加载菜单
王娇
Original
702 people have browsed it

学习总结

1.为了避免频繁和数据库交互,在数据库中查询菜单时,先把有的菜单取出后通过扩展的DB->lists()转为数组后存在数组变量中

2.如果pid===0并且ishidden===0代表需要显示在菜单项中的一级菜单,把这一类存储在一级菜单的变量中

3.如果pid!==0并且ishidden===0代表需要显示在菜单项中的子菜单,把这一类存储在二级菜单的变量中,通过pid分组存储

4.视图渲染时,添加一级菜单时,只需要找到二级菜单对应的pid,遍历数据即可

1.home控制器Home.php

  1. <?php
  2. namespace App\Http\Controllers\admins;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. //引入数据库查询构造器,链式调用
  6. use Illuminate\Support\Facades\DB;
  7. //后台主页
  8. class Home extends Controller
  9. {
  10. public function index()
  11. {
  12. $menus = DB::table('admin_menu')->lists();
  13. $res = [];
  14. foreach($menus as $menu ):
  15. //ishidden为0代表该显示该菜单
  16. //pid等于0的并且的为顶级菜单存储在pmenus中
  17. if($menu['pid']=='0' && $menu['ishidden']=='0'):
  18. $res['pmenus'][] = $menu;
  19. endif;
  20. //pid不等于0的为子菜单,按照pid进行分组存储在cmenus中
  21. if(($menu['pid']!='0')&&($menu['ishidden']=='0')):
  22. $res['cmenus'][$menu['pid']][] = $menu;
  23. endif;
  24. endforeach;
  25. // echo '<pre>';
  26. // print_r($res);
  27. // exit;
  28. return view('admins/home/index',$res);
  29. }
  30. public function welcome()
  31. {
  32. return view('admins/home/welcome');
  33. }
  34. }
  35. ?>

2.后台首页,自动加载数据库的菜单index.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
  7. <script src="/static/plugins/layui/layui.js"></script>
  8. <title>后台首页</title>
  9. <style>
  10. body {
  11. margin: 0px;
  12. padding: 0px;
  13. box-sizing: border-box;
  14. }
  15. /* 头部 */
  16. .header {
  17. width: 100%;
  18. height: 50px;
  19. line-height: 50px;
  20. background-color: #01AAED;
  21. color: #fff;
  22. display: flex;
  23. flex-flow: row nowrap;
  24. justify-content: space-between;
  25. }
  26. .header h3 {
  27. margin-left: 20px;
  28. }
  29. .header>div {
  30. margin-right: 20px;
  31. }
  32. .header>div>a {
  33. color: #fff;
  34. }
  35. .header>div>a:hover {
  36. color: orange;
  37. }
  38. /* 侧边栏和主页面显示区 */
  39. .container {
  40. display: flex;
  41. flex-flow: row nowrap;
  42. justify-content: space-between;
  43. align-items: center;
  44. }
  45. /* 侧边栏 */
  46. .aside {
  47. width: 200px;
  48. background-color: #01AAED;
  49. }
  50. .aside>.layui-collapse {
  51. width: 200px;
  52. }
  53. .aside .layui-colla-title {
  54. background-color: #01AAED;
  55. color: #fff;
  56. }
  57. .aside .layui-nav {
  58. margin-left: -15px;
  59. margin-top: -10px;
  60. margin-bottom: -10px;
  61. width: 197px;
  62. background-color: #fff;
  63. }
  64. .aside .layui-nav .layui-nav-item {
  65. border-bottom: 1px solid #e2e2e2;
  66. border-right: 1px solid #e2e2e2;
  67. }
  68. .aside .layui-nav .layui-nav-item a {
  69. color: #333;
  70. }
  71. .aside .layui-nav .layui-nav-item a:hover {
  72. background-color: lightseagreen;
  73. }
  74. /* 主页面显示区 */
  75. .main {
  76. width: 100%;
  77. }
  78. .main>iframe {
  79. width: 100%;
  80. }
  81. </style>
  82. </head>
  83. <body>
  84. <!-- 头部 -->
  85. <div class="header">
  86. <h3>瑄然软件后台管理系统</h3>
  87. <div>
  88. <span class="layui-icon layui-icon-username">admin</span>
  89. <a href="">退出</a>
  90. </div>
  91. </div>
  92. <div class="container">
  93. <!-- 侧边栏 -->
  94. <div class="aside">
  95. <div class="layui-collapse" lay-accordion>
  96. @foreach($pmenus as $key => $pmenu)
  97. <div class="layui-colla-item">
  98. <h2 class="layui-colla-title">{{$pmenu['title']}}</h2>
  99. <!-- 默认显示第一个菜单的子菜单 -->
  100. <div class="layui-colla-content {{$key?'':'layui-show'}}">
  101. <!-- <div class="layui-colla-content layui-show"> -->
  102. <ul class="layui-nav layui-nav-tree">
  103. <!-- 找到一级菜单所对应的子菜单 -->
  104. <?php $cmenu = $cmenus[$pmenu['mid']];?>
  105. @foreach($cmenu as $item)
  106. <li class="layui-nav-item">
  107. <a href="/admins/{{$item['controller']}}/{{$item['action']}}"
  108. target="showMain">{{$item['title']}}</a>
  109. </li>
  110. @endforeach
  111. </ul>
  112. </div>
  113. </div>
  114. @endforeach
  115. </div>
  116. </div>
  117. <!-- 主页面显示区 -->
  118. <div class="main">
  119. <iframe src="/admins/home/welcome" frameborder="0" name="showMain"></iframe>
  120. </div>
  121. </div>
  122. </body>
  123. <script>
  124. layui.use(['element', 'layer'], function() {
  125. var element = layui.element;
  126. $ = layui.jquery;
  127. var aside_height = document.documentElement.clientHeight - 50;
  128. $('.aside').height(aside_height);
  129. //主页面的高度和侧边栏高度相同
  130. $('.main>iframe').height(aside_height);
  131. });
  132. </script>
  133. </html>

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:态度非常的认真, 继续
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post