Home  >  Article  >  Backend Development  >  How to implement search function using laravel framework

How to implement search function using laravel framework

不言
不言Original
2018-06-13 17:19:512887browse

This article mainly introduces the search function implemented by the laravel framework. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

The search function here is mainly implemented based on the form get submission. The

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

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

The above is the entire content of this article, I hope It will be helpful for everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Learning about database migration in Laravel 5

How the Laravel framework implements the model layer CURD operation

The above is the detailed content of How to implement search function using laravel framework. 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