zoukankan      html  css  js  c++  java
  • Laravel实现列表展示+redis优化+多条件精确查询

    控制器:

    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>
  • 相关阅读:
    jsp与spring mvc后台controller间参数传递处理之总结
    又一个无效的列类型错误Error setting null for parameter #7 with JdbcType NULL . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLExcept
    SSM文件上传要点总结
    关于SSM中mybatis向oracle添加语句采用序列自增的问题
    oracle和mysql的一些区别
    mapper.xml实现oracle的分页语句
    2.数组的解构赋值
    3.Vue 实例
    2.Vue.js 是什么
    1. vue 的安装
  • 原文地址:https://www.cnblogs.com/jiangshiguo/p/13447085.html
Copyright © 2011-2022 走看看