laravel7 权限菜单设置简易升缩
作者:互联网
1:数据库填写数据,pid=0为顶级菜单,pid=对应的id为顶级菜单的子菜单
2:laravel7 创建模型并链接对应的表名
<?php namespace App\models; use Illuminate\Database\Eloquent\Model; class Node extends Model { // protected $table='node'; }
3:定义一个Base控制器,用于继承,里头书写递归方法:
<?php namespace App\Http\Controllers\admin; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class Base extends Controller { //递归处理层级 public function sonlevel($data, $pid = 0) { $arr = []; foreach ($data as $key => $val) { if ($val['pid'] == $pid) { $val['son'] = $this->sonlevel($data, $val['id']); $arr[] = $val; } } return $arr; } }
4:创建控制器:继承Base控制器,并将数据渲染至页面
<?php namespace App\Http\Controllers\admin; use App\Http\Controllers\Controller; use App\models\Node; use Illuminate\Http\Request; class Index extends Base { //展示Node模型数据列表 public function index(){ $data=Node::all(); //调用Base控制器的方法 $datas=$this->sonlevel($data); //将数据渲染至视图 return view('admin.index.index',compact('datas')); } }
5:视图进行双循环
<aside class="Hui-aside"> <div class="menu_dropdown bk_2"> @foreach($datas as $item) <dl id="menu-article"> <dt><i class="Hui-iconfont"></i> {{$item['node_name']}}<i class="Hui-iconfont menu_dropdown-arrow"></i></dt> <dd> <ul> @foreach($item['son'] as $sons) <li><a data-href="article-list.html" data-title="资讯管理" href="javascript:void(0)">{{$sons['node_name']}}</a></li> @endforeach </ul> </dd> </dl> @endforeach </aside>
效果图:
标签:控制器,菜单,val,laravel7,pid,视图,item,升缩 来源: https://www.cnblogs.com/xiaoyantongxue/p/15551149.html