public function lists(Request $request){
$where = [];
//条件搜索
if(!empty($request['type'])){
$where['type'] = $request['type'];
}
if(!empty($request['title'])){
$where['title'] = $request['title'];
}
if(!empty($request['author'])){
$where['author'] = $request['author'];
}
if($where){
//根据条件进行查询
$data = Articles::lists2($where);
$page = $data->currentPage();//当前页
$num = $data->lastPage();//总页数
return view('twelve.list',['arr'=>$data,'msg'=>'数据搜索','page'=>$page,'num'=>$num]);
}
//接收当前页
$page = empty($request['page']) ? 1: $request['page'];
//拦截
$json = Redis::get("article$page");
if($json){
$arr = json_decode($json,true);
//在数组中提取想要的数据
$tableData = $arr['data'];//表格内容
$num = $arr['last_page'];//总页数
return view('twelve.list',['arr'=>$tableData,'num'=>$num,'msg'=>'Redis查询成功','page'=>$page]);
}
//先分页查询出数据(对象)
$data = Articles::lists();
//先将对象想办法转化成数组(这里采用json互换的形式)
$json = json_encode($data);
$arr = json_decode($json,true);
//在数组中提取想要的数据
$tableData = $arr['data'];//表格内容
$num = $arr['last_page'];//总页数
//加入redis
Redis::set("article$page",$json);
//展示页面
return view('twelve.list',['arr'=>$tableData,'num'=>$num,'msg'=>'Mysql查询成功','page'=>$page]);
}
模型层:
static public function lists(){
return self::paginate(10);
}
static public function lists2($where){
return self::where($where)->paginate(10);
}
视图层:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>列表</title>
<link rel="stylesheet" href="css/bs.css">
</head>
<body>
<h1>{{$msg}}</h1>
<form action="">
分类:<input type="text" name="type">
标题:<input type="text" name="title">
作者:<input type="text" name="author">
<input type="submit" value="搜索">
</form>
<table class="table">
<tr>
<th>主键ID</th>
<th>分类</th>
<th>标题</th>
<th>章节</th>
<th>作者</th>
<th>时间</th>
<th>操作</th>
</tr>
@foreach($arr as $k=>$v)
<tr>
<td>{{$v['id']}}</td>
<td>{{$v['type']}}</td>
<td>{{$v['title']}}</td>
<td>{{$v['last_title']}}</td>
<td>{{$v['author']}}</td>
<td>{{$v['time']}}</td>
<td>
<a href="">删除</a>
</td>
</tr>
@endforeach
</table>
<a href="lists?page=1">首页</a>
<a href="lists?page={{$page-1<=1 ? 1 : $page-1}}">上一页</a>
<a href="lists?page={{$page+1>=$num ? $num : $page+1}}">下一页</a>
<a href="lists?page={{$num}}">尾页</a>
</body>
</html>
优秀的背后都是加倍的努力!!!