Paginator

There are several ways to paginate items. The simplest is by using the paginate method on the query builder.

Paginating Database Results

$users = DB::table('users')->paginate(15);

The argument passed to the paginate method is the number of items you wish to display per page. Once you have retrieved the results, you may display them on your view, and create the pagination links using the links method:

<div class="container">
<?php foreach ($users as $user): ?>
<?php echo $user->name; ?>
<?php endforeach; ?>
</div> <?php echo $users->links(); ?>

You may also access additional pagination information via the following methods:

  • getCurrentPage
  • getLastPage
  • getPerPage
  • getTotal
  • getFrom
  • getTo
  • count

"Simple Pagination"

If you are only showing "Next" and "Previous" links in your pagination view, you have the option of using the simplePaginate method to perform a more efficient query. This is useful for larger datasets when you do not require the display of exact page numbers on your view:

$someUsers = DB::table('users')->where('votes', '>', 100)->simplePaginate(15);

Creating A Paginator Manually

Sometimes you may wish to create a pagination instance manually, passing it an array of items. You may do so using the Paginator::make method:

$paginator = Paginator::make($items, $totalItems, $perPage);

Appending To Pagination Links

You can add to the query string of pagination links using the appends method on the Paginator:

<?php echo $users->appends(array('sort' => 'votes'))->links(); ?>

This will generate URLs that look something like this:

http://example.com/something?page=2&sort=votes

If you wish to append a "hash fragment" to the paginator's URLs, you may use the fragment method:

<?php echo $users->fragment('foo')->links(); ?>

This method call will generate URLs that look something like this:

http://example.com/something?page=2#foo

Full Example Usage

Users Model

namespace App\Models;

use Database\Model;

class Users extends Model
{
/**
* The table associated with the Model.
*
* @var string
*/
protected $table = 'users'; /**
* The primary key for the Model.
*
* @var string
*/
protected $primaryKey = 'id'; /**
* The number of Records to return for pagination.
*
* @var int
*/
protected $perPage = 25;
}

Users Controller

namespace App\Controllers;

use Core\View;
use Core\Controller; class Users extends Controller
{ private $model; public function __construct()
{
parent::__construct(); $this->model = new \App\Models\Users();
} public function dashboard()
{
$users = $this->model->paginate(); return View::make('Users/Dashboard')
->shares('title', 'Dashboard')
->with('users', $users);
}
}

Users Dashboard View

<?php foreach ($users->getItems() as $user): ?>
<?php echo $user->username; ?>
<?php endforeach ?> <?php echo $users->links() ?>

Paginator的更多相关文章

  1. bootstrap分页插件--Bootstrap Paginator的使用&AJAX版备份(可单独使用)

    html部分: <ul class="pagination"></ul> <!--bootstrap3版本用ul包裹--> <div cl ...

  2. Bootstrap Paginator 分页插件参数介绍及使用

    Bootstrap Paginator是一款基于Bootstrap的js分页插件,功能很丰富,个人觉得这款插件已经无可挑剔了.它提供了一系列的参数用来支持用户的定制,提供了公共的方法可随时获得插件状态 ...

  3. Bootstrap Paginator分页插件的使用

    今天,我为大家带来的一款做得非常优秀的分页插件BootStrap Paginator,他是一款js插件,由于本人也是才刚刚搞出来的,所以暂时对它也不是特别了解,只能大楖告诉大家怎么使用.我这里使用的是 ...

  4. Bootstrap-分页插件Paginator

    Bootstrap Paginator是一款基于Bootstrap的js分页插件,功能很丰富,个人觉得这款插件已经无可挑剔了.它提供了一系列的参数用来支持用户的定制,提供了公共的方法可随时获得插件状态 ...

  5. bootstrap paginator 与 bootstrap3兼容

    bootstrap paginator可支持bootstrap2 和bootstrap3. 默认的下载包中支持2,需要手动修改才能支持bootstrap3.具体方法:找到bootstrap-pagin ...

  6. Bootstrap Paginator分页插件

    Bootstrap Paginator分页插件使用示例 最近做的asp.netMVC项目中需要对数据列表进行分类,这个本来就是基于bootstrap开发的后台,因此也就想着bootstrap是否有分页 ...

  7. CakePHP下使用paginator需要对多个字段排序的做法

      原文:http://blog.csdn.net/kunshan_shenbin/article/details/7644603  CakePHP下使用paginator需要对多个字段排序的做法 2 ...

  8. Django中扩展Paginator实现分页

    Reference:https://my.oschina.net/kelvinfang/blog/134342 Django中已经实现了很多功能,基本上只要我们需要的功能,都能够找到相应的包.要在Dj ...

  9. Bootstrap Paginator分页插件+ajax 实现动态无刷新分页

    之前做分页想过做淘宝的那个,但是因为是后台要求不高,就Bootstrap Paginator插件感觉还蛮容易上手,所以就选了它. Bootstrap Paginator分页插件下载地址: Downlo ...

随机推荐

  1. 【原】android通过adb wireless的使用

    开发android程序,总是需要插拔插拔的,usb口都给弄坏掉了,现在adb可以通过无线网,链接处在同一个局域网下面的android设备 1.将电脑和移动设备链接到同一个无线网下 2.在android ...

  2. Hibernate之QBC检索和本地SQL检索

    QBC查询就是通过使用Hibernate提供的Query By Criteria API来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口 本地SQL查询来完善HQL ...

  3. js基础第八天

    返回前面起第一个字符位置 indexOf("sdfsdf");它是从左边索引为0开始数,而且只找第一个,然后返回该字符的位置.返回是个数值.如果找不到该字符,那么就会返回-1. 返 ...

  4. 使用 cloc 统计代码行数

    可能大家都知道用 `wc -l` 命令进行代码行数统计,但是它会将代码中的注释.空行所占用的文本行都统计在内.如果想查看一个 tar 包或一个项目目录中“实际”的代码行数并且不愿意自己去写一个脚本来做 ...

  5. NOIP2012 Vigenère 密码

    1.Vigenère 密码 (vigenere.cpp/c/pas) [问题描述] 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法——Vigenère 密码.V ...

  6. rsync 的安装

    Server setup 0)yum -y install xinetd vi /etc/xinetd.d/rsync and ensure following:        disable = n ...

  7. [Objective-c 基础 - 3.1] 内存管理

    A.内存存放.retain.release 1.栈内存:存放局部变量,运行超过变量作用域自后编译器自动回收 2.堆内存:存放对象(地址,对象实体) 3.对象的基本结构 (1)引用计数器(4字节):当计 ...

  8. 转载有个小孩跟我说LINQ(重点讲述Linq中GroupBy的原理及用法)

    转载原出处: http://www.cnblogs.com/AaronYang/archive/2013/04/02/2994635.html 小孩LINQ系列导航:(一)(二)(三)(四)(五)(六 ...

  9. PHP+MySQL开发技术详解—学习笔记

    1.      PHP is Hypertext Preproocessor. 2.      Hello World: <?php Echo ‘Hello World!’; ?> 3.  ...

  10. 射击的乐趣:WIN32诠释打飞机游戏源码补充

    打飞机游戏源码补充 从指定位置加载bmp并显示到对话框. , TRUE);, , LR_LOADFROMFILE);          {         BITMAP bmpinfo;        ...