53.添加新增,编辑,删除权限方法
/app/Http/Controllers/Admin/PermissionController.php
先增加添加方法
class PermissionController extends Controller { // ................ /** * 添加权限功能 * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ public function create() { $permissions = $this->tree(); return view('admin.permission.create',compact('permissions')); } /** * 处理新增加的权限功能 * @param PermissionCreateRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(Request $request) { $name = $request->input('name'); $display_name = $request->input('display_name'); $parent_id = $request->input('parent_id'); $data = ['name' => $name,'display_name' => $display_name, 'parent_id' => $parent_id]; if(Permission::create($data)){ return redirect()->to(route('admin.permission'))->with(['status' => '添加权限成功']); }else{ return redirect()->to(route('admin.permission'))->withErrors(['status' => '系统错误']); } } }
54.分别添加上面对应的模板
新增模板
/resources/views/admin/permission/create.blade.php
@extends('admin.base') @section('content') <div class="layui-card"> <div class="layui-card-header layuiadmin-card-header-auto"> <h2>添加权限</h2> </div> <div class="layui-card-body"> <form class="layui-form" action="{{route('admin.permission.store')}}" method="post"> @include('admin.permission._from') </form> </div> </div> @endsection /resources/views/admin/permission/_from.blade.php {{csrf_field()}} <div class="layui-form-item"> <label for="" class="layui-form-label">父级</label> <div class="layui-input-block"> <select name="parent_id" lay-search> <option value="0">顶级权限</option> @forelse($permissions as $perm) <option value="{{$perm['id']}}" {{ isset($permission->id) && $perm['id'] == $permission->parent_id ? 'selected' : '' }} >{{$perm['display_name']}}</option> @if(isset($perm['_child'])) @foreach($perm['_child'] as $childs) <option value="{{$childs['id']}}" {{ isset($permission->id) && $childs['id'] == $permission->parent_id ? 'selected' : '' }} > ┗━━{{$childs['display_name']}}</option> @if(isset($childs['_child'])) @foreach($childs['_child'] as $lastChilds) <option value="{{$lastChilds['id']}}" {{ isset($permission->id) && $lastChilds['id'] == $permission->parent_id ? 'selected' : '' }} > ┗━━{{$lastChilds['display_name']}}</option> @endforeach @endif @endforeach @endif @empty @endforelse </select> </div> </div> <div class="layui-form-item"> <label for="" class="layui-form-label">显示名称</label> <div class="layui-input-block"> <input type="text" name="display_name" value="{{$permission->display_name??old('display_name')}}" lay-verify="required" class="layui-input" placeholder="如:系统管理"> </div> </div> <div class="layui-form-item"> <label for="" class="layui-form-label">路由</label> <div class="layui-input-block"> <input class="layui-input" type="text" name="name" value="{{$permission->name??old('name')}}" placeholder="如:admin.member" > </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button type="submit" class="layui-btn" lay-submit="" >确 认</button> <a href="{{route('admin.permission')}}" class="layui-btn" >返 回</a> </div> </div>
至此,即权限可以添加了
55.添加编辑权限模板
/resources/views/admin/permission/edit.blade.php @extends('admin.base') @section('content') <div class="layui-card"> <div class="layui-card-header layuiadmin-card-header-auto"> <h2>更新权限</h2> </div> <div class="layui-card-body"> <form class="layui-form" action="{{route('admin.permission.update',['id'=>$permission->id])}}" method="post"> {{method_field('put')}} <input type="hidden" name="id" value="{{ $permission->id }}"> @include('admin.permission._from') </form> </div> </div> @endsection
56.方法中添加对应的方法
/app/Http/Controllers/Admin/PermissionController.php
// /** * 编辑权限 * @param $id * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ public function edit($id) { $permission = Permission::findOrFail($id); $permissions = $this->tree(); return view('admin.permission.edit',compact('permissions','permission')); } /** * 保存权限修改 * @param Request $request * @param $id * @return \Illuminate\Http\RedirectResponse */ public function update(Request $request, $id) { $permission = Permission::findOrFail($id); $data = []; $name = $request->get('name'); $display_name = $request->get('display_name'); $parent_id = $request->get('parent_id'); $data['name'] = $name; $data['display_name'] = $display_name; $data['parent_id'] = $parent_id; if ($permission->update($data)){ return redirect()->to(route('admin.permission'))->with(['status'=>'更新成功']); } return redirect()->to(route('admin.permission'))->withErrors('系统错误'); } /** * 删除对应的权限 * @param Request $request * @return \Illuminate\Http\JsonResponse */ public function destroy(Request $request) { $ids = $request->get('ids'); if (empty($ids)){ return response()->json(['code'=>1,'msg'=>'请选择删除项']); } if (Permission::destroy($ids)){ return response()->json(['code'=>0,'msg'=>'删除成功']); } return response()->json(['code'=>1,'msg'=>'删除失败']); }