V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yyy123456
V2EX  ›  PHP

Eloquent 分页组件 pagination 单独使用完全攻略(原创)

  •  
  •   yyy123456 · 2017-07-06 12:52:44 +08:00 · 2925 次点击
    这是一个创建于 2492 天前的主题,其中的信息可能已经有所发展或是发生改变。

    为了单独使用 Eloquent 分页,当然得先使用 Eloquent 库,

    本代码完全脱离 lavavel5.4 环境。只加载最新的 5.4 类库,故意不使用模板,让代码含义更加清晰。

    composer

    "illuminate/database":"5.4.27" ,
    "illuminate/events":"5.4.27",
    "illuminate/pagination":"5.4.27"
    

    建表

    CREATE TABLE `test_databases` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `db_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '库名',
      `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '测试用户 id',
      `created_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
      `updated_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '修改时间',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB
    

    请自行插入一百条数据。^_^

    假设本机项目域名 www.t3.com

    本代码网址 http://www.t3.com/paginator/ill

    首页只需输入上面网址即可,点击第 2 页,会自动加 page 查询参数。

    代码中各种解释应该比较清楚了。

    <?php
    namespace app\control;
    use \Illuminate\Database\Capsule\Manager as Capsule;
    // use \Illuminate\Events\Dispatcher;
    // use \Illuminate\Container\Container;
    use \Illuminate\Pagination\UrlWindow;
    class Paginator {
       
     public function ill( $req, $res, $args) {
           $capsule = new Capsule;
           $capsule->addConnection([
               'driver'    => 'mysql',
               'host'      => '127.0.0.1',
               'database'  => 'test1',
               'username'  => 'root',
               'password'  => 'root',
               'charset'   => 'utf8mb4',
               'collation' => 'utf8mb4_unicode_ci',
               'prefix'    => '',
           ]);
           $capsule->setAsGlobal();
           $conn =$capsule; 
           echo "<h1>Eloquent 分页使用</h1>";
          
           $order  = "id" ;// 这里定义排序字段。
           $page = intval( $_GET["page"]);
           if (!$page) {
               $page=1;
           }
           
           $per_page = 4; //每页显示 4 条结果
           
           //获取结果
           $paginator = $conn::table('test_databases')->select(['db_name','user_id'])->orderBy($order, 'asc')
             ->limit(100)
             ->paginate($per_page, ['*'],'page',$page);
          //先显示结果集
          foreach ($paginator as $v) {
              echo $v->db_name . " = " . $v->user_id."<br>";
          }
          
          //必须设置网址
           $paginator->setPath('/paginator/ill');
          //设置其他 query 参数
          // $users->appends('order', $order);
          
           // 得到 laravel 构建的链接结果,是数组。分 3 部分,first,slider,  last
           $win = new UrlWindow($paginator);
           // 2×3 +1 就是显示在中间的链接个数,
           // 也可以不填写的,默认是 3.
           $url_arr = $win->get(3);
                  
           if ($paginator->hasPages()) { //有结果集才显示啊
               if (!$paginator->onFirstPage()) {
                   echo "<a href='{$paginator->previousPageUrl()}'>上页</a>"."&nbsp;&nbsp;";
               }
               
               if (isset( $url_arr['first'] )) {
                   foreach ($url_arr['first'] as $k=> $v ) {
                       $style=" ";
                       if ($k == $paginator->currentPage()) {
                           $style=" style='color:red' ";
                       }
                       echo "<a {$style} href='{$v}'>$k</a>"."&nbsp;&nbsp;";
                   }
               }
               
               if (isset( $url_arr['slider'] )) {
                   echo '...'; // 这样页面漂亮些。
                   foreach ($url_arr['slider'] as $k=> $v ) {
                       $style=" ";
                       if ($k == $paginator->currentPage()) {
                           $style=" style='color:red' ";
                       }
                       echo "<a {$style}  href='{$v}'>$k</a>"."&nbsp;&nbsp;";
                   }
               }
               
               if (isset( $url_arr['last'] )) {
                   echo '...'; // 这样页面漂亮些。
                   foreach ($url_arr['last'] as $k=> $v ) {
                       $style=" ";
                       if ($k == $paginator->currentPage()) {
                           $style=" style='color:red' ";
                       }
                       echo "<a  {$style} href='{$v}'>$k</a>"."&nbsp;&nbsp;";
                   }
               }
               
               if ($paginator->lastPage()!=$page) {
                   echo "<a href='{$paginator->nextPageUrl()}'>下页</a>"."&nbsp;&nbsp;";
               }
               
           }else {
               echo "没查到数据";
           }
       }
    }
    

    展示效果

    图片

    图片 图片 图片

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2260 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:13 · PVG 14:13 · LAX 23:13 · JFK 02:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.