Maison  >  Article  >  développement back-end  >  code d'implémentation de la fonction de recherche du framework Laravel

code d'implémentation de la fonction de recherche du framework Laravel

小云云
小云云original
2018-05-11 11:10:581804parcourir

Cet article partage principalement avec vous le code d'implémentation de la fonction de recherche du framework Laravel, dans l'espoir d'aider tout le monde. La fonction de recherche ici est principalement implémentée sur la base de la soumission d'un formulaire.

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

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

Recommandations associées :

Explication sur l'implémentation de la fonction de recherche simple PHP

Expliquez comment JS implémente la fonction de recherche Baidu

Fonction de recherche PHP

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn