Pagination
Pagination Method Based on Laravel ORM
Laravel's illuminate/database
provides convenient pagination functionality.
Installation
composer require illuminate/pagination
Usage
public function index(Request $request)
{
$per_page = 10;
$users = Db::table('user')->paginate($per_page);
return view('index/index', ['users' => $users]);
}
Paginator Instance Methods
Method | Description |
---|---|
$paginator->count() | Get the total number of items on the current page |
$paginator->currentPage() | Get the current page number |
$paginator->firstItem() | Get the index of the first item in the result set |
$paginator->getOptions() | Get the paginator options |
$paginator->getUrlRange($start, $end) | Create URLs for the specified page range |
$paginator->hasPages() | Checks if there are enough items to create multiple pages |
$paginator->hasMorePages() | Checks if there are more pages to show |
$paginator->items() | Get the items for the current page |
$paginator->lastItem() | Get the index of the last item in the result set |
$paginator->lastPage() | Get the page number of the last page (not available in simplePaginate) |
$paginator->nextPageUrl() | Get the URL for the next page |
$paginator->onFirstPage() | Checks if the current page is the first page |
$paginator->perPage() | Get the total number of items displayed per page |
$paginator->previousPageUrl() | Get the URL for the previous page |
$paginator->total() | Get the total number of items in the result set (not available in simplePaginate) |
$paginator->url($page) | Get the URL for the specified page |
$paginator->getPageName() | Get the query parameter name used to store page number |
$paginator->setPageName($name) | Set the query parameter name used to store page number |
Note
The$paginator->links()
method is not supported
Pagination Component
In webman, the $paginator->links()
method cannot be used to render pagination buttons; however, we can use other components to render it, such as jasongrimes/php-paginator
.
Installation
composer require "jasongrimes/paginator:~1.0"
Backend
<?php
namespace app\controller;
use JasonGrimes\Paginator;
use support\Request;
use support\Db;
class UserController
{
public function get(Request $request)
{
$per_page = 10;
$current_page = $request->input('page', 1);
$users = Db::table('user')->paginate($per_page, '*', 'page', $current_page);
$paginator = new Paginator($users->total(), $per_page, $current_page, '/user/get?page=(:num)');
return view('user/get', ['users' => $users, 'paginator' => $paginator]);
}
}
Template (PHP Native)
Create the template app/view/user/get.html
<html>
<head>
<!-- Built-in support for Bootstrap pagination styles -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<?= $paginator;?>
</body>
</html>
Template (Twig)
Create the template app/view/user/get.html
<html>
<head>
<!-- Built-in support for Bootstrap pagination styles -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
{% autoescape false %}
{{paginator}}
{% endautoescape %}
</body>
</html>
Template (Blade)
Create the template app/view/user/get.blade.php
<html>
<head>
<!-- Built-in support for Bootstrap pagination styles -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
{!! $paginator !!}
</body>
</html>
Template (ThinkPHP)
Create the template app/view/user/get.html
<html>
<head>
<!-- Built-in support for Bootstrap pagination styles -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<?=$paginator?>
</body>
</html>
The effect is as follows:
Pagination Method Based on ThinkPHP ORM
No additional library installation is required; just install think-orm.
Usage
public function index(Request $request)
{
$per_page = 10;
$users = Db::table('user')->paginate(['list_rows' => $per_page, 'page' => $request->get('page', 1), 'path' => $request->path()]);
return view('index/index', ['users' => $users]);
}
Template (ThinkPHP)
<html>
<head>
<!-- Built-in support for Bootstrap pagination styles -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
{$users|raw}
</body>
</html>