laravel框架关于搜索功能的实现
这里的搜索功能主要基于表单get提交实现的
选择分类: 全部 @foreach($categoryas$c) id}}">{{$c->class_name}} @endforeach 文章标题:
php部分逻辑
publicfunctionarticle_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(); //渲染页面传递参数 returnview('backend.article_list',['postdata'=>$postdata,'shownum'=>$shownum,'category'=>$category]); }