本文是作者整理的关于laravel框架搜索功能的实现原理,并附上了详细代码,有需要的小伙伴请持续关注!
这里的搜索功能主要基于表单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]);
}
沃梦达教程
本文标题为:laravel框架关于搜索功能的实现
猜你喜欢
- laravel通用化的CURD的实现 2023-03-17
- PHP仿tp实现mvc框架基本设计思路与实现方法分析 2022-10-18
- PHP实现微信支付(jsapi支付)流程步骤详解 2022-10-09
- PHP中PDO事务处理操作示例 2022-10-15
- PHP简单实现二维数组的矩阵转置操作示例 2022-10-02
- Laravel balde模板文件中判断数据为空方法 2023-08-30
- php微信公众号开发之秒杀 2022-11-23
- windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境) 2023-09-02
- laravel实现按月或天或小时统计mysql数据的方法 2023-02-22
- 用nohup命令实现PHP的多进程 2023-09-02