• laravel8.5与layuiadmin整合(11)


    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' : '' }} >&nbsp;&nbsp;┗━━{{$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' : '' }} >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;┗━━{{$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'=>'删除失败']);
        }
  • 相关阅读:
    poj1273
    JavaSE入门学习23:Java面向对象之构造方法
    NOI 2015 滞后赛解题报告
    LuaInterface简单介绍
    解决在onCreate()过程中获取View的width和Height为0的4种方法
    函数指针和指针函数
    Quartz-中断正在执行的任务
    servlet3.0获取参数与文件上传代码示例
    Servlet学习:(三)Servlet3.0 上传文件
    layui 批量上传文件 + 后台 用servlet3.0接收【我】
  • 原文地址:https://www.cnblogs.com/lin3615/p/15803005.html
Copyright © 2020-2023  润新知