# :-: 加载首页代码 ~~~ //用户列表展示 public function index(){ $users = \app\admin\model\Users::all(); static $roles=[] ; foreach( $users as $v){ $v['role_id'] =explode(',',$v['role_id']); } //循环数据页面展示效果 foreach($users as $v) { foreach($v['role_id'] as $c){ $roles[] = \app\admin\model\Role::where('id',$c)->find(); } } return view('',compact('users','roles')); } ~~~ ![](https://box.kancloud.cn/0c7715ed7e65b67eacbf2177d3b94371_2986x424.png) # :-: Html 代码 ~~~ {include file='common/father_css'/} <body class="gray-bg"> <div class="wrapper wrapper-content animated fadeInRight"> <div class="row"> <div class="col-sm-12"> <div class="ibox float-e-margins"> <div class="ibox-title"> <h5>所有栏目</h5> </div> <div class="ibox-content"> <div class="table-responsive"> <form name="myform" id="myform" method="post"> <table class="table table-striped"> <thead> <tr> <th>id</th> <th>账号</th> <th>是否是管理员</th> <th>权限列表</th> <th>操作</th> </tr> </thead> <tbody> {foreach $users as $v} <tr> <td> {$v['id']} </td> <td> {$v.usename} </td> <td > {eq name='$v->is_admin' value='1'}管理员{/eq} {eq name='$v->is_admin' value='0'}普通会员{/eq} </td> <td> {foreach $roles as $c} <span class="text-danger">{$c->role_name},</span> {/foreach} </td> <td> <div class="layui-table-cell laytable-cell-1-0-10"> <a class="layui-btn layui-btn-xs" lay-event="edit" href="{:url('admin/admin/member',['id'=>$v['id']])}">编辑</a> <a class="layui-btn layui-btn-xs" lay-event="edit" href="{:url('admin/users/add',['id'=>$v['id']])}">设置权限 </a> {eq name='$v->id' value='session('uid')'} <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" onclick="del({$v.id})">删除</a> <a class="layui-btn layui-btn-xs" lay-event="edit" href="{:url('admin/users/add',['id'=>$v['id']])}">设置权限 </a> {/eq} </div> </td> </tr> {/foreach} </tbody> </table> </form> </div> </div> </div> </div> </div> </div> <!-- 全局js --> <script> function del(id) { swal({ title: "确定删除吗?", text: "你将无法回复该用户,点击空白区,取消选框", type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "确定删除!", cancelButtonText: "取消删除!", closeOnConfirm: false, closeOnCancel: false }).then( function(isConfirm){ if (isConfirm) { //或者id ,传递给后台 window.location.href = "/admin/friendly/delete/id/"+id } else { swal("取消!", "你的权限删除结束)", "error"); } }); } </script> </body> </html> ~~~ # :-: 添加权限代码 ~~~ //用户添加角色 public function add(Request $request){ $id = $request->param('id'); $roles = \app\admin\model\Role::all(); return view('',compact('roles','id')); } //用户添加角色修改 public function addSave(Request $request){ $data = $request->param(); $id = $request->param('id'); $data['role_id'] = implode(',',$data['role_id']); unset($data['id']); $info = Db::table('user')->where('id',$id)->data($data)->update(); if ($info){ $this->success('角色添加成功','/admin/users/index'); } } ~~~ # :-: Html 代码 ~~~ {include file='common/father_css'/} <body class="gray-bg"> <div class="wrapper wrapper-content animated fadeInRight"> <div class="row"> <div class="col-sm-12"> <div class="ibox float-e-margins"> <div class="ibox-title"> <h5 class="text-danger">选择角色</h5> </div> <div class="ibox-content"> <div class="table-responsive"> <form method="post" class="form-horizontal layui-form" action="{:url('admin/users/addSave')}"> <input type="hidden" name="id" value="{$id}"> <div class="layui-form-item"> <div class="layui-input-block"> {foreach $roles as $role} <input type="checkbox" name="role_id[]" title="{$role.role_name}" value="{$role.id}" > {/foreach} </div> </div> <div class="hr-line-dashed"></div> <div class="col-sm-12 text-center"> <button class="btn-sm btn-danger layui-btn-radius" type="submit">保存角色</button> </div> </form> </div> </div> </div> </div> </div> </div> <!-- 全局js --> <script> layui.use(['form', 'layedit', 'laydate'], function(){ var form = layui.form ,layer = layui.layer ,layedit = layui.layedit ,laydate = layui.laydate; //日期 laydate.render({ elem: '#date' }); laydate.render({ elem: '#date1' }); //创建一个编辑器 var editIndex = layedit.build('LAY_demo_editor'); //自定义验证规则 form.verify({ title: function(value){ if(value.length < 5){ return '标题至少得5个字符啊'; } } ,pass: [ /^[\S]{6,12}$/ ,'密码必须6到12位,且不能出现空格' ] ,content: function(value){ layedit.sync(editIndex); } }); //监听指定开关 form.on('switch(switchTest)', function(data){ layer.msg('开关checked:'+ (this.checked ? 'true' : 'false'), { offset: '6px' }); layer.tips('温馨提示:请注意开关状态的文字可以随意定义,而不仅仅是ON|OFF', data.othis) }); //监听提交 form.on('submit(demo1)', function(data){ layer.alert(JSON.stringify(data.field), { title: '最终的提交信息' }) return false; }); //表单初始赋值 form.val('example', { "username": "贤心" // "name": "value" ,"password": "123456" ,"interest": 1 ,"like[write]": true //复选框选中状态 ,"close": true //开关状态 ,"sex": "女" ,"desc": "我爱 layui" } }); </script> </body> </html> ~~~ ![](https://box.kancloud.cn/5ad11b25801ad926a1689193ef8d871c_3310x426.png)