本篇内容介绍了“laravel怎么实现添加角色和模糊搜索功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1.1 角色添加显示页面路由(get)
// 角色添加页面显示 Route::get('role/add', 'RoleController@addindex') -> name('admin.role.add');
1.2 角色添加页面控制器(get)
public function addindex () { return view('admin.role.add'); }
1.3 角色添加模版
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name="renderer" content="webkit|ie-comp|ie-stand"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" /> <meta http-equiv="Cache-Control" content="no-siteapp" /> <link rel="Bookmark" href="/favicon.ico" rel="external nofollow" rel="external nofollow" > <link rel="Shortcut Icon" href="/favicon.ico" rel="external nofollow" rel="external nofollow" /> <link rel="stylesheet" type="text/css" href="/admin/static/h-ui/css/H-ui.min.css" rel="external nofollow" /> <link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/css/H-ui.admin.css" rel="external nofollow" /> <link rel="stylesheet" type="text/css" href="/admin/lib/Hui-iconfont/1.0.8/iconfont.css" rel="external nofollow" /> <link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/skin/default/skin.css" rel="external nofollow" id="skin" /> <link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/css/style.css" rel="external nofollow" /> <title>添加角色</title> </head> <body> <article class="page-container"> <!-- 表单验证 --> @include('admin.common.validate') <form action="{{route('admin.role.add')}}" method="post" class="form form-horizontal" id="form-member-add"> @csrf <div class="row cl"> <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色名:</label> <div class="formControls col-xs-8 col-sm-9"> <input type="text" class="input-text" placeholder="角色名" id="name" name="name"> </div> </div> <div class="row cl"> <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3"> <input class="btn btn-primary radius" type="submit" value=" 添加 "> </div> </div> </form> </article> <!--_footer 作为公共模版分离出去--> <script type="text/javascript" src="/admin/lib/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript" src="/admin/lib/layer/2.4/layer.js"></script> <script type="text/javascript" src="/admin/static/h-ui/js/H-ui.min.js"></script> <script type="text/javascript" src="/admin/static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去--> <!--请在下方写此页面业务相关的脚本--> <script type="text/javascript" src="/admin/lib/My97DatePicker/4.8/WdatePicker.js"></script> <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script> <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/validate-methods.js"></script> <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/messages_zh.js"></script> </body> </html>
效果:
1.4 角色添加控制器(post)
public function add (Request $request) { $data = $request -> except(['_token']); Role::create($data); // 跳转到列表页 return redirect(route('admin.role.index')) -> with('success', '添加角色成功'); }
效果:
修改index
控制器方法:
public function index (Request $request) { // 获取搜索框 分页 $kw = $request -> get('kw'); $data = Role::when($kw, function ($query) use($kw) { $query->where('name', 'like', "%{$kw}%"); }) -> paginate($this->pagesize); // dump($data); return view('admin.role.index', compact('data')); }
效果:
“laravel怎么实现添加角色和模糊搜索功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。