欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-09-26 12:31:38
这里的搜索功能主要基于表单get提交实现的
&l...

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

<form action="/backend/auditlist" method="get">
  <table class="search_tab">
    <tr>
      <th width="120">选择分类:</th>
      <td>
        <select name="class" >
          <option value="">全部</option>
          @foreach($category as $c)
            <option value="{{$c->id}}">{{$c->class_name}}</option>
          @endforeach
        </select>
      </td>
      <th width="70">文章标题:</th>
      <!--查询关键词-->
      <td><input type="text" name="keywords" placeholder="文章标题"></td>
      <td><input type="submit" name="sub" value="查询"></td>
    </tr>
  </table>
</form>

php部分逻辑

public function article_list(){
  //echo 'zoule';exit; 测试表单是否走进方法中 大家随意写
  $shownum = 1;
  if(array_key_exists('class',$_get)||array_key_exists('keywords',$_get)){
   //echo '111'; 
   if($_get['class']){
   //article模型 leftjoin表连接 查询根据俩个表里的这些字段来执行
     $postdata = article::leftjoin('category', function($join) {
      $join->on('article.class_id', '=', 'category.id');
     })->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);
   }elseif($_get['keywords']){
     $postdata = article::leftjoin('category', function($join) {
      $join->on('article.class_id', '=', 'category.id');
     })->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);
    
   }else{
     $postdata = article::leftjoin('category', function($join) {
      $join->on('article.class_id', '=', 'category.id');
     })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->orderby('release_time','desc')->paginate($shownum);
   }
  }else{
   //echo '2222';
   $postdata = article::leftjoin('category', function($join) {
     $join->on('article.class_id', '=', 'category.id');
   })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->orderby('release_time','desc')->paginate($shownum);
  }
  //分类id不是父id
  $category = db::table('category')->where('parent_id','!=','0')->get();
  //渲染页面 传递 参数
  return view('backend.article_list',['postdata'=>$postdata,'shownum'=>$shownum,'category'=>$category]);
}