首页 技术 正文
技术 2022年11月14日
0 收藏 422 点赞 4,183 浏览 2382 个字

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() ?>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,466
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,095
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765