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

來源:互聯網
上載者:User

這裡的搜尋功能主要基於表單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 'zoule';exit; 測試表單是否走進方法中 大家隨意寫  $shownum = 1;  if(array_key_exists('class',$_GET)||array_key_exists('keywords',$_GET)){   //echo '111';    if($_GET['class']){   //Article模型 leftJoin表串連 查詢根據倆個表裡的這些欄位來執行     $postdata = Article::leftJoin('category', function($join) {      $join->on('article.class_id', '=', 'category.id');     })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->where('article.class_id','=',$_GET['class'])->orderBy('release_time','desc')->paginate($shownum);   }elseif($_GET['keywords']){     $postdata = Article::leftJoin('category', function($join) {      $join->on('article.class_id', '=', 'category.id');     })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->where('article.title_editing','=',$_GET['keywords'])->orderBy('release_time','desc')->paginate($shownum);       }else{     $postdata = Article::leftJoin('category', function($join) {      $join->on('article.class_id', '=', 'category.id');     })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->orderBy('release_time','desc')->paginate($shownum);   }  }else{   //echo '2222';   $postdata = Article::leftJoin('category', function($join) {     $join->on('article.class_id', '=', 'category.id');   })->select(['article.id','category.class_name','article.status','article.title_editing','article.update_time'])->orderBy('release_time','desc')->paginate($shownum);  }  //分類id不是父id  $category = DB::table('category')->where('parent_id','!=','0')->get();  //渲染頁面 傳遞 參數  return view('backend.article_list',['postdata'=>$postdata,'shownum'=>$shownum,'category'=>$category]);}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.