1.运用cmd在项目根目录下创建路由组
php artisan make:controller queryController --resource
1.1数据库信息(student)
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(225) NOT NULL, `age` tinyint(3) unsigned NOT NULL, `sex` tinyint(3) unsigned NOT NULL, `created_at` int(11) NOT NULL, `updated_at` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
2.路由组信息如下
3.编写路由组中的 index和store方法(1是显示首页,2是模糊查询)运用的是orWhere();添加多条件查询
1 <?php 2 3 namespace App\Http\Controllers; 4 5 use Illuminate\Http\Request; 6 use think\View; 7 8 class queryController extends Controller 9 { 10 /** 11 * Display a listing of the resource. 12 * 13 * @return \Illuminate\Http\Response 14 */ 15 public function index() 16 { 17 //显示查询界面 18 $data = \DB::table('student')->get(); 19 return view('query', compact('data')); 20 } 21 22 /** 23 * Show the form for creating a new resource. 24 * 25 * @return \Illuminate\Http\Response 26 */ 27 public function create() 28 { 29 echo '创建'; 30 } 31 32 /** 33 * Store a newly created resource in storage. 34 * 35 * @param \Illuminate\Http\Request $request 36 * @return \Illuminate\Http\Response 37 */ 38 public function store(Request $request) 39 { 40 //返回请求结果界面 41 $input = $request->get('key'); 42 $data = \DB::table('student')->where('id', 'like', '%' . $input . '%') 43 ->orWhere('name', 'like', '%' . $input . '%')->orWhere('age', 'like', '%' . $input . '%')->get(); 44 if(!is_null($data)){ 45 return view('query', compact('data')); 46 }else{ 47 return back()->with(['info'=>'查询为空']); 48 } 49 } 50 51 /** 52 * Display the specified resource. 53 * 54 * @param int $id 55 * @return \Illuminate\Http\Response 56 */ 57 public function show($id) 58 { 59 // 60 } 61 62 /** 63 * Show the form for editing the specified resource. 64 * 65 * @param int $id 66 * @return \Illuminate\Http\Response 67 */ 68 public function edit($id) 69 { 70 // 71 } 72 73 /** 74 * Update the specified resource in storage. 75 * 76 * @param \Illuminate\Http\Request $request 77 * @param int $id 78 * @return \Illuminate\Http\Response 79 */ 80 public function update(Request $request, $id) 81 { 82 // 83 } 84 85 /** 86 * Remove the specified resource from storage. 87 * 88 * @param int $id 89 * @return \Illuminate\Http\Response 90 */ 91 public function destroy($id) 92 { 93 // 94 } 95 }多条件模糊查询
4.视图层代码
1 <!doctype html> 2 <html lang="en"> 3 <head style="background-color: lightpink"> 4 <meta charset="UTF-8"> 5 <meta name="viewport" 6 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> 9 </script> 10 <title>多条件模糊查询</title> 11 </head> 12 <body> 13 <form action="{{route('query.store')}}" method="post"> 14 {{@csrf_field()}} 15 <input type="text"size="20px" name="key" placeholder="请输入id,用户名,年龄其一"> 16 <input type="submit" value="搜索" > 17 </form> 18 19 <table border="1px solid" cellspacing="0" cellpadding="0"style=" margin-top: 50px;width: auto;"> 20 <tr> 21 <th>id</th> 22 <th>用户名</th> 23 <th>年龄</th> 24 <th>性别</th> 25 <th>创建时间</th> 26 <th>更新时间</th> 27 </tr> 28 @foreach($data as $values) 29 <tr> 30 <td>{{$values->id}}</td> 31 <td>{{$values->name}}</td> 32 <td>{{$values->age}}</td> 33 <td> 34 @if(($values->sex==1))男 35 @else女 36 @endif 37 </td> 38 39 <td>{{date('Y-m-d H:i:s',$values->created_at)}}</td> 40 <td>{{date('Y-m-d H:i:s',$values->updated_at)}}</td> 41 </tr> 42 @endforeach 43 </table> 44 </body> 45 46 </html>视图层
5.个人总结
1.模板中post提交的时候注意 {{@csrf_field()}} 不要忘记了 2.想起了三种定位路由的方式 2.1 {{action('你的路由action')}} {{url('')}} {{route()}} 3.判断数据库信息返回是否为空:is_null(); empty()
|