Home  >  Article  >  Backend Development  >  laravel framework search function implementation code

laravel framework search function implementation code

小云云
小云云Original
2018-05-11 11:10:581858browse

This article mainly shares with you the laravel framework search function implementation code, hoping to help everyone. The search function here is mainly implemented based on form get submission.

<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>

Partial logic of 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]);
}

Related recommendations:

Explanation on the implementation of simple search function in PHP

JS explained with examples How to implement Baidu search function

php search function

The above is the detailed content of laravel framework search function implementation code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn