Home  >  Article  >  Backend Development  >  laravel framework implements search function code analysis

laravel framework implements search function code analysis

php中世界最好的语言
php中世界最好的语言Original
2018-05-18 11:00:541661browse

This time I will bring you the code analysis of the laravel framework to implement the search function. What are the precautions for laravel framework to implement the search function. The following is a practical case, let's take a look.

The search function here is mainly based on the

                                                         
选择分类:                文章标题:

php partial logic implemented by form get submission

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

I believe you have mastered the method after reading the case in this article. Please pay attention for more exciting things. Other related articles on php Chinese website!

Recommended reading:

ThinkPHP Detailed explanation of the steps to implement WeChat payment (jsapi payment)

##How to operate phpstudy2018 access directory service permissions

PHP namespace namespace definition and import use case analysis

The above is the detailed content of laravel framework implements search function code analysis. 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