首頁 >後端開發 >php教程 >laravel框架關於搜尋功能的實現

laravel框架關於搜尋功能的實現

不言
不言原創
2018-04-19 14:05:402419瀏覽

    這裡的搜尋功能主要基於表單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