laravel rbac的用户 角色 权限的crud
Posted 段佳伟的小憩屋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel rbac的用户 角色 权限的crud相关的知识,希望对你有一定的参考价值。
user.php
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::group([‘middleware‘ => [‘\iqiyi\Http\Middleware\VerifyCsrfToken::class‘]], function () { //支付订单 Route::any(‘order/{action}‘, function(\iqiyi\Http\Controllers\Order $index, $action,\Illuminate\Http\Request $request ){ return $index->$action($request); })->middleware(‘authAdmin‘); // 报告 Route::any(‘report/{action}‘, function(\iqiyi\Http\Controllers\Report $index, $action,\Illuminate\Http\Request $request ){ return $index->$action($request); })->middleware(‘authAdmin‘); // 数据统计 Route::any(‘statistics/{action}‘, function(\iqiyi\Http\Controllers\Statistics $index, $action,\Illuminate\Http\Request $request ){ return $index->$action($request); })->middleware(‘authAdmin‘); // 图片管理 Route::any(‘img/{action}‘, function(\iqiyi\Http\Controllers\Img $index, $action,\Illuminate\Http\Request $request ){ return $index->$action($request); //dump($request); })->middleware(‘authAdmin‘); // 用户管理 Route::any(‘user/{action}‘, function(\iqiyi\Http\Controllers\User $index, $action,\Illuminate\Http\Request $request ){ return $index->$action($request); })->middleware(‘authAdmin‘); // 角色管理 Route::any(‘role/{action}‘, function(\iqiyi\Http\Controllers\Role $index, $action,\Illuminate\Http\Request $request ){ return $index->$action($request); })->middleware(‘authAdmin‘); // 菜单管理 Route::any(‘menu/{action}‘, function(\iqiyi\Http\Controllers\Menu $index, $action,\Illuminate\Http\Request $request ){ return $index->$action($request); })->middleware(‘authAdmin‘); Route::any(‘/login‘, ‘[email protected]‘); Route::any(‘logout‘, ‘[email protected]‘); Route::any(‘/‘, ‘[email protected]‘)->middleware(‘authAdmin‘); Route::any(‘import‘, ‘[email protected]‘)->middleware(‘authAdmin‘); Route::any(‘import/{action}‘,function(\iqiyi\Http\Controllers\Import $index, $action,\Illuminate\Http\Request $request ){ return $index->$action($request); })->middleware(‘authAdmin‘); Route::any(‘stock/{action}‘,function(\iqiyi\Http\Controllers\Stock $index, $action,\Illuminate\Http\Request $request ){ return $index->$action($request); })->middleware(‘authAdmin‘); Route::get(‘downreport/{filename}/{expname}‘, function($filename,$expname) { return response()->download(storage_path(‘report/‘.$filename),$expname.‘.xls‘); })->middleware(‘authAdmin‘); // Route::any(‘channel/{action}‘,function(\iqiyi\Http\Controllers\Channel $index, $action,\Illuminate\Http\Request $request ){ // return $index->$action($request); // })->middleware(‘authAdmin‘); Route::any(‘importlist‘, ‘[email protected]‘)->middleware(‘authAdmin‘); });
对应的blade模板页面 index.blade.php
@extends(‘layouts.layout‘) @section(‘content‘) <div id="content"> <div class="page-header"> <div class="container-fluid"> <div class="pull-right"> <a href="/user/create" data-toggle="tooltip" title="新增" class="btn btn-primary">新增 </a> <button type="button" data-toggle="tooltip" title="删除" class="btn btn-danger" onclick="confirm(‘确认?‘) ? $(‘#form-index‘).submit() : false;"> 删除 </button> </div> <h3 class="htitle">用户管理</h3> </div> </div> <div class="container-fluid"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"> <i class="fa fa-list"></i> 用户列表 </h3> </div> <div class="panel-body"> <form action="/user/delete" method="post" id="form-index"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="table-responsive"> <table class="table table-bordered table-hover"> <thead> <tr> <td style="width: 1px;" class="text-center"> <input type="checkbox" id="select-all" title="全选"/> </td> <th class="text-left">登录名</th> <th class="text-left">姓名</th> <th class="text-left">角色</th> <th class="text-left">手机号 </th> <th class="text-left">电话</th> <th class="text-left">邮箱</th> <th class="text-left">最后登陆时间</th> <th class="text-left">状态</th> <th class="text-right">管理</th> </tr> </thead> <tbody> @foreach ($users as $user) <tr> <td class="text-center"> <input type="checkbox" name="selected[]" value="{{$user->ids}}"/> </td> <td class="text-left">{{$user->login_name}}</td> <td class="text-left">{{$user->name}}</td> <td class="text-left">{{$user->role}}</td> <td class="text-left">{{$user->phone}}</td> <td class="text-left">{{$user->mobile}}</td> <td class="text-left">{{$user->email}}</td> <td class="text-left">{{$user->login_date}}</td> <td class="text-left"> @if ($user->status === 0) 无效 @elseif ($user->status === 1) 有效 @else 待审核 @endif </td> <td class="text-right"> <a href="/user/edit?ids={{$user->ids}}" data-toggle="tooltip" title="编辑" class="btn btn-primary">编辑 </a> </td> </tr> @endforeach </tbody> </table> </div> </form> <div class="pagers "> {{$users->render()}} </div> </div> </div> </div> </div> <script> $(function() { $(‘#select-all‘).click(function() { // 找到需要操作的记录的checkbox,控制状态, 为当前全选checkbox的checked状态 // :checkbox == input[type=checkbox] $(‘:checkbox[name="selected[]"]‘).prop(‘checked‘, $(this).prop(‘checked‘)); }); }); </script> @endsection
create.blade.php
@extends(‘layouts.layout‘) @section(‘content‘) <div id="content"> <div class="page-header"> <div class="container-fluid"> <div class="pull-right"> <button type="submit" form="form-set" data-toggle="tooltip" title="保存" class="btn btn-primary">保存 <i class="fa fa-save"></i> </button> <a href="/user/index" data-toggle="tooltip" title="取消" class="btn btn-default">取消 <i class="fa fa-reply"></i> </a> </div> </div> </div> <div class="container-fluid"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"> <i class="fa fa-pencil"></i> @if(isset($id)) 编辑 @else 添加 @endif 用户 </h3> </div> <div class="panel-body"> <form action="/user/create" method="post" enctype="multipart/form-data" id="form-set" class="form-horizontal"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="tab-content"> <div class="tab-pane active" id="tab-general"> <div class="form-group "> <label class="col-sm-2 control-label" for="input-login-name">登录名</label> <div class="col-sm-10"> <input type="text" required="required" name="login_name" value="{{$user->login_name or ‘‘}}" placeholder=" 登录名" id="input-login-name" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-name">姓名</label> <div class="col-sm-10"> <input type="text"required="required" name="name" value="{{$user->name or ‘‘}}" placeholder=" 姓名" id="input-name" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-password_md5">密码 </label> <div class="col-sm-10"> <input type="password" required="required" name="password_md5" value="{{$user->password or ‘‘}}" placeholder=" 密码 " id="input-password_md5" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-role">角色</label> <div class="col-sm-10"> <select class="form-control" name="role"> @foreach($roles as $role) <option value ="{{$role->ids}}">{{$role->name}}</option> @endforeach </select> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-phone">手机号</label> <div class="col-sm-10"> <input type="text" required="required" name="phone" value="{{$user->phone or ‘‘}}" placeholder=" 手机号" id="input-phone" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-mobile">电话</label> <div class="col-sm-10"> <input type="text" required="required" name="mobile" value="{{$user->mobile or ‘‘}}" placeholder=" 电话" id="input-mobile" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-email">邮箱 </label> <div class="col-sm-10"> <input type="text" required="required" name="email" value="{{$user->email or ‘‘}}" placeholder=" 邮箱 " id="input-email" class="form-control"/> </div> </div> </div> </div> </form> </div> </div> </div> </div> @endsection
edit.blade.php
@extends(‘layouts.layout‘) @section(‘content‘) <div id="content"> <div class="page-header"> <div class="container-fluid"> <div class="pull-right"> <button type="submit" form="form-set" data-toggle="tooltip" title="保存" class="btn btn-primary">保存 <i class="fa fa-save"></i> </button> <a href="/user/index" data-toggle="tooltip" title="取消" class="btn btn-default">取消 <i class="fa fa-reply"></i> </a> </div> </div> </div> <div class="container-fluid"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"> <i class="fa fa-pencil"></i> 编辑用户 </h3> </div> <div class="panel-body"> <form action="/user/update?ids={{$user->ids}}" method="post" enctype="multipart/form-data" id="form-set" class="form-horizontal"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="tab-content"> <div class="tab-pane active" id="tab-general"> <div class="form-group "> <label class="col-sm-2 control-label" for="input-login-name">登录名</label> <div class="col-sm-10"> <input type="text" required="required" name="login_name" value="{{$user->login_name or ‘‘}}" placeholder=" 登录名" id="input-login-name" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-name">姓名</label> <div class="col-sm-10"> <input type="text" required="required" name="name" value="{{$user->name or ‘‘}}" placeholder=" 姓名" id="input-name" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-password_md5">密码 </label> <div class="col-sm-10"> <input type="password" required="required" name="password_md5" value="{{$user->password_md5 or ‘‘}}" placeholder=" 密码 " id="input-password_md5" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-role">角色</label> <div class="col-sm-10"> <select class="form-control" name="role"> @foreach($roles as $role) <option value ="{{$role->ids}}" @if($role->ids == $role_id) selected="selected" @endif>{{$role->name}}</option> @endforeach </select> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-phone">手机号</label> <div class="col-sm-10"> <input type="text" required="required" name="phone" value="{{$user->phone or ‘‘}}" placeholder=" 手机号" id="input-phone" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-mobile">电话</label> <div class="col-sm-10"> <input type="text" required="required" name="mobile" value="{{$user->mobile or ‘‘}}" placeholder=" 电话" id="input-mobile" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-email">邮箱 </label> <div class="col-sm-10"> <input type="text" required="required" name="email" value="{{$user->email or ‘‘}}" placeholder=" 邮箱 " id="input-email" class="form-control"/> </div> </div> </div> </div> </form> </div> </div> </div> </div> @endsection
role.php
<?php /** * 角色管理(增删改查) * Date: 2018/4/12 * Time: 14:24 */ namespace iqiyi\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use iqiyi\Models\SysMenu; class Role extends Controller { /*角色列表索引页*/ public function index(Request $request) { ## 获取分页数据 $roles=DB::table(‘sys_role‘)->orderBy(‘ids‘,‘asc‘)->paginate(10); // dd($roles); ## 响应视图 return view(‘role.index‘) ->with(‘roles‘, $roles) ; } public function create(Request $request) { # 展示表单 if ($request->isMethod(‘get‘)) { $menus=DB::table(‘sys_menu‘)->get(); $sysMenu = new SysMenu(); $menusTree = $sysMenu->getTreeMenu($menus, $p_id = 0, $level = 0); // dd($menus, $menusTree); return view(‘role.create‘) ->with(‘menusTree‘, $menusTree) ; } # 入库数据 elseif ($request->isMethod(‘post‘)) { $roles = $request->input(‘roles‘); $input = $request->except(‘_token‘, ‘roles‘); $input[‘create_time‘] = date(‘Y-m-d H:i:s‘); $role_ids = DB::table(‘sys_role‘)->insertGetId($input); $role_menu = []; foreach($roles as $k=>$role) { $role_menu[$k][‘role_ids‘] = $role_ids; $role_menu[$k][‘menu_id‘] = $role; } // dd($roles, $input, $role_menu); # 同步role_ids对应的menu_id到sys_role_menu if ($role_ids) { $res = DB::table(‘sys_role_menu‘)->insert($role_menu); if ($res) { # 重定向到index return redirect(‘role/index‘); } } } } public function edit(Request $request) { $ids = $request->input(‘ids‘); $role = DB::table(‘sys_role‘)->where(‘ids‘, $ids)->get(); var_dump($ids, $role); ## 获取菜单列表 $menus=DB::table(‘sys_menu‘)->get(); $sysMenu = new SysMenu(); $menusTree = $sysMenu->getTreeMenu($menus, $p_id = 0, $level = 0); $menus=DB::table(‘sys_role_menu‘)->where(‘role_ids‘, $ids)->pluck(‘menu_id‘); return view(‘role.edit‘) ->with(‘role‘, $role[0]) ->with(‘menus‘, $menus) ->with(‘menusTree‘, $menusTree) ; } public function update(Request $request) { # 更新数据 $ids = $request->input(‘ids‘); $roles = $request->input(‘roles‘); $input = $request->except(‘_token‘, ‘roles‘, ‘ids‘); $input[‘create_time‘] = date(‘Y-m-d H:i:s‘); // dd($ids,$roles,$input); $reslut = DB::table(‘sys_role‘)->where(‘ids‘, $ids)->update($input); if ($reslut) { $res = DB::table(‘sys_role_menu‘)->where(‘role_ids‘, $ids)->delete(); if ($res) { $role_menu = []; foreach($roles as $k=>$role) { $role_menu[$k][‘role_ids‘] = $ids; $role_menu[$k][‘menu_id‘] = $role; } $resu = DB::table(‘sys_role_menu‘)->insert($role_menu); if ($resu) { # 重定向到index return redirect(‘role/index‘); } } } } public function delete(Request $request) { $ids = $request->input(‘selected‘); if (!empty($ids)) { $res = DB::table(‘sys_role‘)->whereIn(‘ids‘, $ids)->delete(); } # 重定向到index return redirect(‘role/index‘); } }
index.blade.php
@extends(‘layouts.layout‘) @section(‘content‘) <div id="content"> <div class="page-header"> <div class="container-fluid"> <div class="pull-right"> <a href="/role/create" data-toggle="tooltip" title="新增" class="btn btn-primary">新增 </a> <button type="button" data-toggle="tooltip" title="删除" class="btn btn-danger" onclick="confirm(‘确认?‘) ? $(‘#form-index‘).submit() : false;"> 删除 </button> </div> <h3 class="htitle">角色管理</h3> </div> </div> <div class="container-fluid"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"> <i class="fa fa-list"></i> 角色列表 </h3> </div> <div class="panel-body"> <form action="/role/delete" method="post" id="form-index"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="table-responsive"> <table class="table table-bordered table-hover"> <thead> <tr> <td style="width: 1px;" class="text-center"> <input type="checkbox" id="select-all" title="全选"/> </td> <th class="text-left">角色名称</th> <th class="text-left">角色说明</th> <th class="text-left">角色创建时间</th> <th class="text-right">管理</th> </tr> </thead> <tbody> @foreach ($roles as $role) <tr> <td class="text-center"> <input type="checkbox" name="selected[]" value="{{$role->ids}}"/> </td> <td class="text-left">{{$role->name}}</td> <td class="text-left">{{$role->remarks}}</td> <td class="text-left">{{$role->create_time}}</td> <td class="text-right"> <a href="/role/edit?ids={{$role->ids}}" data-toggle="tooltip" title="编辑" class="btn btn-primary">编辑 </a> </td> </tr> @endforeach </tbody> </table> </div> </form> <div class="pagers "> {{$roles->render()}} </div> </div> </div> </div> </div> <script> $(function() { $(‘#select-all‘).click(function() { // 找到需要操作的记录的checkbox,控制状态, 为当前全选checkbox的checked状态 // :checkbox == input[type=checkbox] $(‘:checkbox[name="selected[]"]‘).prop(‘checked‘, $(this).prop(‘checked‘)); }); }); </script> @endsection
create.blade.php
@extends(‘layouts.layout‘) @section(‘content‘) <div class="pull-left right_content " style="margin-left: 20px;margin-top: 20px;padding: 0;"> <style> .tree { min-height:20px; padding:19px; margin-bottom:20px; background-color:#fbfbfb; border:1px solid #999; -webkit-border-radius:4px; -moz-border-radius:4px; border-radius:4px; -webkit-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05); -moz-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05); box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05) } .tree li { list-style-type:none; margin:0; padding:10px 5px 0 5px; position:relative } .tree li::before, .tree li::after { content:‘‘; left:-20px; position:absolute; right:auto } .tree li::before { border-left:1px solid #999; bottom:50px; height:100%; top:0; width:1px } .tree li::after { border-top:1px solid #999; height:20px; top:25px; width:25px } .tree li span { -moz-border-radius:5px; -webkit-border-radius:5px; border:1px solid #999; border-radius:5px; display:inline-block; padding:3px 8px; text-decoration:none } .tree li.parent_li>span { cursor:pointer } .tree>ul>li::before, .tree>ul>li::after { border:0 } .tree li:last-child::before { height:30px } .tree li.parent_li>span:hover, .tree li.parent_li>span:hover+ul li span { background:#eee; border:1px solid #94a0b4; color:#000 } </style> <form id="form-set" action="/role/create" method="post"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="tree well"> <lable>角色名称:</lable> <input type=‘text‘ required="required" name=‘name‘ value=""> <lable>角色说明:</lable> <input type=‘text‘ name=‘remarks‘ required="required" value=""> {{--<input type="submit" value="保存">--}} <button type="submit" form="form-set" data-toggle="tooltip" title="保存" class="btn btn-primary">保存 <i class="fa fa-save"></i> </button> <a href="/role/index" data-toggle="tooltip" title="取消" class="btn btn-default">取消 <i class="fa fa-reply"></i> </a> <ul> <li> <b>+</b><span><i class="icon-folder-open"></i>全部<input id="all" type="checkbox"></span> <ul class="all"> @foreach($menusTree as $menu) @if($menu->level == 0) <li><b>+</b><span><i class="icon-folder-open"></i>{{$menu->name}}<input type="checkbox" name="roles[]" value="{{$menu->id}}"></span></li> @endif @if($menu->level == 1) <ul> <li><span><i class="icon-minus-sign"></i>{{$menu->name}}<input type="checkbox" name="roles[]" value="{{$menu->id}}"></span></li> </ul> @endif @if($menu->level == 2) <ul> <ul> <li> <span><i class="icon-minus-sign"></i>{{$menu->name}}<input type="checkbox" name="roles[]" value="{{$menu->id}}"></span> </li> </ul> </ul> @endif @endforeach </ul> </li> </ul> </div> </form> <script> $(function(){ $(‘.tree li:has(ul)‘).addClass(‘parent_li‘).find(‘ > span‘).attr(‘title‘, ‘Collapse this branch‘); $(‘.tree li.parent_li > b‘).on(‘click‘, function (e) { var children = $(this).parent(‘li.parent_li‘).find(‘ > ul > li‘); if (children.is(":visible")) { children.hide(‘fast‘); $(this).attr(‘title‘, ‘Expand this branch‘).find(‘ > i‘).addClass(‘icon-plus-sign‘).removeClass(‘icon-minus-sign‘); } else { children.show(‘fast‘); $(this).attr(‘title‘, ‘Collapse this branch‘).find(‘ > i‘).addClass(‘icon-minus-sign‘).removeClass(‘icon-plus-sign‘); } e.stopPropagation(); }); $(‘input[type="checkbox"]‘).click(function(){ var t = $(this).is(‘:checked‘); if ( t == true ){ $(this).parent().parent().parent().parent().find(‘input‘).first().parent().parent().parent().parent().find(‘input‘).first().prop(‘checked‘,true); $(this).parent().parent().parent().parent().find(‘input‘).first().prop(‘checked‘,true); $(this).parent().parent().find(‘ul‘).find(‘input‘).prop(‘checked‘,true); $(this).next(‘ul‘).find(‘input‘).prop(‘checked‘,true); } else { $(‘#all‘).prop(‘checked‘,false); $(this).parent().parent().find(‘ul‘).find(‘input‘).prop(‘checked‘,false); /*if($(this).parent().parent().parent().find("input:checked").length<1){ $(this).parent().parent().parent().parent().find(‘input‘).first().prop(‘checked‘,false); }*/ } if($(‘.all‘).find("input").length==$(‘.all‘).find("input:checked").length){ $(‘#all‘).prop(‘checked‘,true); }else{ $(‘#all‘).prop(‘checked‘,false); } }); }) </script> </div> @endsection
edite.blade.php
@extends(‘layouts.layout‘) @section(‘content‘) <div class="pull-left right_content " style="margin-left: 20px;margin-top: 20px;padding: 0;"> <style> .tree { min-height:20px; padding:19px; margin-bottom:20px; background-color:#fbfbfb; border:1px solid #999; -webkit-border-radius:4px; -moz-border-radius:4px; border-radius:4px; -webkit-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05); -moz-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05); box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05) } .tree li { list-style-type:none; margin:0; padding:10px 5px 0 5px; position:relative } .tree li::before, .tree li::after { content:‘‘; left:-20px; position:absolute; right:auto } .tree li::before { border-left:1px solid #999; bottom:50px; height:100%; top:0; width:1px } .tree li::after { border-top:1px solid #999; height:20px; top:25px; width:25px } .tree li span { -moz-border-radius:5px; -webkit-border-radius:5px; border:1px solid #999; border-radius:5px; display:inline-block; padding:3px 8px; text-decoration:none } .tree li.parent_li>span { cursor:pointer } .tree>ul>li::before, .tree>ul>li::after { border:0 } .tree li:last-child::before { height:30px } .tree li.parent_li>span:hover, .tree li.parent_li>span:hover+ul li span { background:#eee; border:1px solid #94a0b4; color:#000 } </style> <form id="form-set" action="/role/update?ids={{$role->ids}}" method="post"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="tree well"> <lable>角色名称:</lable> <input type=‘text‘ required="required" name=‘name‘ value="{{$role->name}}"> <lable>角色说明:</lable> <input type=‘text‘ name=‘remarks‘ required="required" value="{{$role->remarks}}"> {{--<input type="submit" value="保存">--}} <button type="submit" form="form-set" data-toggle="tooltip" title="保存" class="btn btn-primary">保存 <i class="fa fa-save"></i> </button> <a href="/role/index" data-toggle="tooltip" title="取消" class="btn btn-default">取消 <i class="fa fa-reply"></i> </a> <ul> <li> <b>+</b><span><i class="icon-folder-open"></i>全部<input id="all" type="checkbox"></span> <ul class="all"> @foreach($menusTree as $menu) @if($menu->level == 0) <li><b>+</b><span><i class="icon-folder-open"></i>{{$menu->name}}<input type="checkbox" name="roles[]" value="{{$menu->id}}" @foreach($menus as $menuone) @if($menu->id == $menuone) checked="checked" @endif @endforeach ></span></li> @endif @if($menu->level == 1) <ul> <li><span><i class="icon-minus-sign"></i>{{$menu->name}}<input type="checkbox" name="roles[]" value="{{$menu->id}}" @foreach($menus as $menuone) @if($menu->id == $menuone) checked="checked" @endif @endforeach ></span></li> </ul> @endif @if($menu->level == 2) <ul> <ul> <li> <span><i class="icon-minus-sign"></i>{{$menu->name}}<input type="checkbox" name="roles[]" value="{{$menu->id}}" @foreach($menus as $menuone) @if($menu->id == $menuone) checked="checked" @endif @endforeach ></span> </li> </ul> </ul> @endif @endforeach </ul> </li> </ul> </div> </form> <script> $(function(){ $(‘.tree li:has(ul)‘).addClass(‘parent_li‘).find(‘ > span‘).attr(‘title‘, ‘Collapse this branch‘); $(‘.tree li.parent_li > b‘).on(‘click‘, function (e) { var children = $(this).parent(‘li.parent_li‘).find(‘ > ul > li‘); if (children.is(":visible")) { children.hide(‘fast‘); $(this).attr(‘title‘, ‘Expand this branch‘).find(‘ > i‘).addClass(‘icon-plus-sign‘).removeClass(‘icon-minus-sign‘); } else { children.show(‘fast‘); $(this).attr(‘title‘, ‘Collapse this branch‘).find(‘ > i‘).addClass(‘icon-minus-sign‘).removeClass(‘icon-plus-sign‘); } e.stopPropagation(); }); $(‘input[type="checkbox"]‘).click(function(){ var t = $(this).is(‘:checked‘); if ( t == true ){ $(this).parent().parent().parent().parent().find(‘input‘).first().parent().parent().parent().parent().find(‘input‘).first().prop(‘checked‘,true); $(this).parent().parent().parent().parent().find(‘input‘).first().prop(‘checked‘,true); $(this).parent().parent().find(‘ul‘).find(‘input‘).prop(‘checked‘,true); $(this).next(‘ul‘).find(‘input‘).prop(‘checked‘,true); } else { $(‘#all‘).prop(‘checked‘,false); $(this).parent().parent().find(‘ul‘).find(‘input‘).prop(‘checked‘,false); /*if($(this).parent().parent().parent().find("input:checked").length<1){ $(this).parent().parent().parent().parent().find(‘input‘).first().prop(‘checked‘,false); }*/ } if($(‘.all‘).find("input").length==$(‘.all‘).find("input:checked").length){ $(‘#all‘).prop(‘checked‘,true); }else{ $(‘#all‘).prop(‘checked‘,false); } }); }) </script> </div> @endsection
menu.php
<?php /** * 菜单管理 * Date: 2018/4/12 * Time: 17:00 */ namespace iqiyi\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use iqiyi\Models\SysMenu; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; class Menu extends Controller { /*菜单列表索引页*/ public function index(Request $request) { ## 获取数据 $menus=DB::table(‘sys_menu‘)->get(); // $menusPage=DB::table(‘sys_menu‘)->paginate(10); $sysMenu = new SysMenu(); $menusTree = $sysMenu->getTreeMenu($menus, $p_id = 0, $level = 0); $perPage = 10; if ($request->has(‘page‘)) { $current_page = $request->input(‘page‘); $current_page = $current_page <= 0 ? 1 :$current_page; } else { $current_page = 1; } $item = array_slice($menusTree, ($current_page-1)*$perPage, $perPage); //注释1 $total = count($menusTree); $paginator =new LengthAwarePaginator($item, $total, $perPage, $current_page, [ ‘path‘ => Paginator::resolveCurrentPath(), //注释2 ‘pageName‘ => ‘page‘, ]); $menuslist = $paginator->toArray()[‘data‘]; return view(‘menu.index‘, compact(‘menuslist‘, ‘paginator‘)); } public function create(Request $request) { # 展示表单 if ($request->isMethod(‘get‘)) { $first_menus = DB::table(‘sys_menu‘)->where(‘parent_id‘, 0)->get(); return view(‘menu.create‘) ->with(‘first_menus‘, $first_menus) ; } # 入库数据 elseif ($request->isMethod(‘post‘)) { $input = $request->except(‘_token‘); // dd($input); $res = DB::table(‘sys_menu‘)->insert($input); # 重定向到index return redirect(‘menu/index‘); } } public function edit(Request $request) { $id = $request->input(‘id‘); $menu = DB::table(‘sys_menu‘)->where(‘id‘, $id)->get(); $parent_id = $menu[0]->parent_id; // echo $parent_id; $first_menus = DB::table(‘sys_menu‘)->where(‘parent_id‘, 0)->get(); $parent_menus = DB::table(‘sys_menu‘)->where(‘id‘, $parent_id)->get(); // dd($menu); // $role_ids = DB::table(‘sys_menu_role‘)->where(‘menu_ids‘, $ids)->pluck(‘role_ids‘); // $roles = DB::table(‘sys_role‘)->get(); // $role_id = DB::table(‘sys_role‘)->where(‘ids‘, $role_ids[‘0‘])->pluck(‘ids‘); return view(‘menu.edit‘) ->with(‘menu‘, $menu[0]) ->with(‘first_menus‘, $first_menus) ->with(‘parent_menus‘, $parent_menus[0]) ; } public function update(Request $request) { # 更新数据 $input = $request->except(‘_token‘); $id = $input[‘id‘]; // dd($input, $id); $res = DB::table(‘sys_menu‘)->where(‘id‘, $id)->update($input); # 重定向到index return redirect(‘menu/index‘); } public function delete(Request $request) { $id = $request->input(‘selected‘); // dd($ids); if (!empty($id)) { $res = DB::table(‘sys_menu‘)->whereIn(‘id‘, $id)->delete(); } # 重定向到index return redirect(‘menu/index‘); } }
index.blade.php
@extends(‘layouts.layout‘) @section(‘content‘) <div id="content"> <div class="page-header"> <div class="container-fluid"> <div class="pull-right"> <a href="/menu/create" data-toggle="tooltip" title="新增" class="btn btn-primary">新增 </a> <button type="button" data-toggle="tooltip" title="删除" class="btn btn-danger" onclick="confirm(‘确认?‘) ? $(‘#form-index‘).submit() : false;"> 删除 </button> </div> <h3 class="htitle">菜单管理</h3> </div> </div> <div class="container-fluid"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"> <i class="fa fa-list"></i> 菜单列表 </h3> </div> <div class="panel-body"> <form action="/menu/delete" method="post" id="form-index"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="table-responsive"> <table class="table table-bordered table-hover"> <thead> <tr> <td style="width: 1px;" class="text-center"> <input type="checkbox" id="select-all" title="全选"/> </td> <th class="text-left">功能菜单名称</th> <th class="text-left">功能菜单路径</th> <th class="text-left">功能菜单路由</th> <th class="text-left">功能菜单描述</th> <th class="text-right">管理</th> </tr> </thead> <tbody> @foreach ($menuslist as $menu) <tr> <td class="text-center"> <input type="checkbox" name="selected[]" value="{{$menu->id}}"/> </td> <td class="text-left"> @if($menu->level == 1) @elseif($menu->level == 2) @elseif($menu->level == 3) @endif {{$menu->name}}</td> <td class="text-left">{{$menu->name_path}}</td> <td class="text-left">{{$menu->href}}</td> <td class="text-left">{{$menu->remarks}}</td> <td class="text-right"> <a href="/menu/edit?id={{$menu->id}}" data-toggle="tooltip" title="编辑" class="btn btn-primary">编辑 </a> </td> </tr> @endforeach </tbody> </table> </div> </form> <div class="pagers "> {!! $paginator->render() !!} </div> </div> </div> </div> </div> <script> $(function() { $(‘#select-all‘).click(function() { // 找到需要操作的记录的checkbox,控制状态, 为当前全选checkbox的checked状态 // :checkbox == input[type=checkbox] $(‘:checkbox[name="selected[]"]‘).prop(‘checked‘, $(this).prop(‘checked‘)); }); }); </script> @endsection
create.blade.php
@extends(‘layouts.layout‘) @section(‘content‘) <div id="content"> <div class="page-header"> <div class="container-fluid"> <div class="pull-right"> <button type="submit" form="form-set" data-toggle="tooltip" title="保存" class="btn btn-primary">保存 <i class="fa fa-save"></i> </button> <a href="/menu/index" data-toggle="tooltip" title="取消" class="btn btn-default">取消 <i class="fa fa-reply"></i> </a> </div> </div> </div> <div class="container-fluid"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"> <i class="fa fa-pencil"></i> 添加菜单 </h3> </div> <div class="panel-body"> <form action="/menu/create" method="post" enctype="multipart/form-data" id="form-set" class="form-horizontal"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="tab-content"> <div class="tab-pane active" id="tab-general"> <div class="form-group "> <label class="col-sm-2 control-label" for="input-name">功能菜单名称</label> <div class="col-sm-10"> <input type="text" required="required" name="name" value="{{$menu->name or ‘‘}}" placeholder="功能菜单名称" id="input-name" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-name_path">功能菜单路径</label> <div class="col-sm-10"> <input type="text" required="required" name="name_path" value="{{$menu->name_path or ‘‘}}" placeholder="功能菜单路径" id="input-name_path" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-remarks">功能菜单描述</label> <div class="col-sm-10"> <input type="text" required="required" name="remarks" value="{{$menu->remarks or ‘‘}}" placeholder="功能菜单描述" id="input-remarks" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-href">功能菜单路由</label> <div class="col-sm-10"> <input type="text" required="required" name="href" value="{{$menu->href or ‘‘}}" placeholder="功能菜单路由" id="input-href" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-parent_id">父级菜单</label> <div class="col-sm-10"> <select class="form-control" name="parent_id"> <option value = 0>无</option> @foreach($first_menus as $first_menu) <option value ="{{$first_menu->id}}">{{$first_menu->name}}</option> @endforeach </select> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-is_show">是否在菜单中显示</label> <div class="col-sm-10"> <select class="form-control" name="is_show"> <option value =1>显示</option> <option value =0>不显示</option> </select> </div> </div> </div> </div> </form> </div> </div> </div> </div> @endsection
edite.blade.php
@extends(‘layouts.layout‘) @section(‘content‘) <div id="content"> <div class="page-header"> <div class="container-fluid"> <div class="pull-right"> <button type="submit" form="form-set" data-toggle="tooltip" title="保存" class="btn btn-primary">保存 <i class="fa fa-save"></i> </button> <a href="/menu/index" data-toggle="tooltip" title="取消" class="btn btn-default">取消 <i class="fa fa-reply"></i> </a> </div> </div> </div> <div class="container-fluid"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"> <i class="fa fa-pencil"></i> 编辑菜单 </h3> </div> <div class="panel-body"> <form action="/menu/update?id={{$menu->id}}" method="post" enctype="multipart/form-data" id="form-set" class="form-horizontal"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="tab-content"> <div class="tab-pane active" id="tab-general"> <div class="form-group "> <label class="col-sm-2 control-label" for="input-name">功能菜单名称</label> <div class="col-sm-10"> <input type="text" required="required" name="name" value="{{$menu->name or ‘‘}}" placeholder="功能菜单名称" id="input-name" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-name_path">功能菜单路径</label> <div class="col-sm-10"> <input type="text" required="required" name="name_path" value="{{$menu->name_path or ‘‘}}" placeholder="功能菜单路径" id="input-name_path" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-remarks">功能菜单描述</label> <div class="col-sm-10"> <input type="text" required="required" name="remarks" value="{{$menu->remarks or ‘‘}}" placeholder="功能菜单描述" id="input-remarks" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-href">功能菜单路由</label> <div class="col-sm-10"> <input type="text" required="required" name="href" value="{{$menu->href or ‘‘}}" placeholder="功能菜单路由" id="input-href" class="form-control"/> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-parent_id">父级菜单</label> <div class="col-sm-10"> <select class="form-control" name="parent_id"> <option value = 0>无</option> @foreach($first_menus as $first_menu) <option value ="{{$first_menu->id}}" @if($first_menu->id == $parent_menus->id) selected="selected" @endif >{{$first_menu->name}}</option> @endforeach </select> </div> </div> <div class="form-group "> <label class="col-sm-2 control-label" for="input-is_show">是否在菜单中显示</label> <div class="col-sm-10"> <select class="form-control" name="is_show"> <option value = 1 @if($menu->is_show == 1) selected="selected" @endif >显示</option> <option value = 0 @if($menu->is_show == 0) selected="selected" @endif >不显示</option> </option> </select> </div> </div> </div> </div> </form> </div> </div> </div> </div> @endsection
<?php
/**
* 菜单管理
* Date: 2018/4/12
* Time: 17:00
*/
namespace iqiyi\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use iqiyi\Models\SysMenu;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
class Menu extends Controller
{
/*菜单列表索引页*/
public function index(Request $request)
{
## 获取数据
$menus=DB::table(‘sys_menu‘)->get();
// $menusPage=DB::table(‘sys_menu‘)->paginate(10);
$sysMenu = new SysMenu();
$menusTree = $sysMenu->getTreeMenu($menus, $p_id = 0, $level = 0);
$perPage = 10;
if ($request->has(‘page‘)) {
$current_page = $request->input(‘page‘);
$current_page = $current_page <= 0 ? 1 :$current_page;
} else {
$current_page = 1;
}
$item = array_slice($menusTree, ($current_page-1)*$perPage, $perPage); //注释1
$total = count($menusTree);
$paginator =new LengthAwarePaginator($item, $total, $perPage, $current_page, [
‘path‘ => Paginator::resolveCurrentPath(), //注释2
‘pageName‘ => ‘page‘,
]);
$menuslist = $paginator->toArray()[‘data‘];
return view(‘menu.index‘, compact(‘menuslist‘, ‘paginator‘));
}
public function create(Request $request)
{
# 展示表单
if ($request->isMethod(‘get‘)) {
$first_menus = DB::table(‘sys_menu‘)->where(‘parent_id‘, 0)->get();
return view(‘menu.create‘)
->with(‘first_menus‘, $first_menus)
;
}
# 入库数据
elseif ($request->isMethod(‘post‘)) {
$input = $request->except(‘_token‘);
// dd($input);
$res = DB::table(‘sys_menu‘)->insert($input);
# 重定向到index
return redirect(‘menu/index‘);
}
}
public function edit(Request $request)
{
$id = $request->input(‘id‘);
$menu = DB::table(‘sys_menu‘)->where(‘id‘, $id)->get();
$parent_id = $menu[0]->parent_id;
// echo $parent_id;
$first_menus = DB::table(‘sys_menu‘)->where(‘parent_id‘, 0)->get();
$parent_menus = DB::table(‘sys_menu‘)->where(‘id‘, $parent_id)->get();
// dd($menu);
// $role_ids = DB::table(‘sys_menu_role‘)->where(‘menu_ids‘, $ids)->pluck(‘role_ids‘);
// $roles = DB::table(‘sys_role‘)->get();
// $role_id = DB::table(‘sys_role‘)->where(‘ids‘, $role_ids[‘0‘])->pluck(‘ids‘);
return view(‘menu.edit‘)
->with(‘menu‘, $menu[0])
->with(‘first_menus‘, $first_menus)
->with(‘parent_menus‘, $parent_menus[0])
;
}
public function update(Request $request)
{
# 更新数据
$input = $request->except(‘_token‘);
$id = $input[‘id‘];
// dd($input, $id);
$res = DB::table(‘sys_menu‘)->where(‘id‘, $id)->update($input);
# 重定向到index
return redirect(‘menu/index‘);
}
public function delete(Request $request)
{
$id = $request->input(‘selected‘);
// dd($ids);
if (!empty($id)) {
$res = DB::table(‘sys_menu‘)->whereIn(‘id‘, $id)->delete();
}
# 重定向到index
return redirect(‘menu/index‘);
}
}
以上是关于laravel rbac的用户 角色 权限的crud的主要内容,如果未能解决你的问题,请参考以下文章