laravel新增角色和模糊搜尋功能的實現程式碼
阿新 • • 發佈:2021-06-24
一、新增角色
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 n程式設計客棧ofollow" 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 www.cppcns.comnofollow" 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> &lwww.cppcns.comt;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')); }
效果:
總結:
when(keywords,callback)
有兩個引數:引數1如果變http://www.cppcns.com量存在,則執行我們的回撥函式。
以上就是laravel新增角色和模糊搜尋的詳細內容,更多關於laravel模糊搜尋的資料請關注我們其它相關文章!