当前位置:首页 > PHP教程 > php高级应用 > 列表

laravel框架关于搜索功能的实现

发布:smiling 来源: PHP粉丝网  添加日期:2018-10-11 13:00:01 浏览: 评论:0 

这里的搜索功能主要基于表单get提交实现的:

  1. <form action="/backend/auditList" method="get"
  2.   <table class="search_tab"
  3.     <tr> 
  4.       <th width="120">选择分类:</th> 
  5.       <td> 
  6.         <select name="class" > 
  7.           <option value="">全部</option> 
  8.           @foreach($category as $c
  9.             <option value="{{$c->id}}">{{$c->class_name}}</option> 
  10.           @endforeach 
  11.         </select> 
  12.       </td> 
  13.       <th width="70">文章标题:</th> 
  14.       <!--查询关键词--> 
  15.       <td><input type="text" name="keywords" placeholder="文章标题"></td> 
  16.       <td><input type="submit" name="sub" value="查询"></td> 
  17.     </tr> 
  18.   </table> 
  19. </form> 

php部分逻辑:

  1. public function article_list(){ 
  2.   //echo 'zoule';exit; 测试表单是否走进方法中 大家随意写 
  3.   $shownum = 1; 
  4.   if(array_key_exists('class',$_GET)||array_key_exists('keywords',$_GET)){ 
  5.    //echo '111';  
  6.    if($_GET['class']){ 
  7.    //Article模型 leftJoin表连接 查询根据俩个表里的这些字段来执行 
  8.      $postdata = Article::leftJoin('category'function($join) { 
  9.       $join->on('article.class_id''=''category.id'); 
  10.      })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->where('article.class_id','=',$_GET['class'])->orderBy('release_time','desc')->paginate($shownum); 
  11.    }elseif($_GET['keywords']){ 
  12.      $postdata = Article::leftJoin('category'function($join) { 
  13.       $join->on('article.class_id''=''category.id'); 
  14.      })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->where('article.title_editing','=',$_GET['keywords'])->orderBy('release_time','desc')->paginate($shownum); 
  15.       
  16.    }else
  17.      $postdata = Article::leftJoin('category'function($join) { 
  18.       $join->on('article.class_id''=''category.id'); 
  19.      })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->orderBy('release_time','desc')->paginate($shownum); 
  20.    } 
  21.   }else
  22.    //echo '2222'; 
  23.    $postdata = Article::leftJoin('category'function($join) { 
  24.      $join->on('article.class_id''=''category.id'); 
  25.    })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->orderBy('release_time','desc')->paginate($shownum); 
  26.   } //phpfensi.com 
  27.   //分类id不是父id 
  28.   $category = DB::table('category')->where('parent_id','!=','0')->get(); 
  29.   //渲染页面 传递 参数 
  30.   return view('backend.article_list',['postdata'=>$postdata,'shownum'=>$shownum,'category'=>$category]); 

Tags: 框架 功能

分享到: