首页 >后端开发 >php教程 >laravel框架关于搜索功能的实现

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

不言
不言原创
2018-04-19 14:05:402432浏览

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

相关推荐:

laravel框架中常用目录路径详解

以上是laravel框架关于搜索功能的实现的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn