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

这里的搜索功能主要基于表单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. }else{
  16. $postdata = Article::leftJoin('category', function($join) {
  17. $join->on('article.class_id', '=', 'category.id');
  18. })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->orderBy('release_time','desc')->paginate($shownum);
  19. }
  20. }else{
  21. //echo '2222';
  22. $postdata = Article::leftJoin('category', function($join) {
  23. $join->on('article.class_id', '=', 'category.id');
  24. })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->orderBy('release_time','desc')->paginate($shownum);
  25. } //phpfensi.com
  26. //分类id不是父id
  27. $category = DB::table('category')->where('parent_id','!=','0')->get();
  28. //渲染页面 传递 参数
  29. return view('backend.article_list',['postdata'=>$postdata,'shownum'=>$shownum,'category'=>$category]);
  30. }