pagination.md 3.08 KB
Newer Older
Taylor Otwell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# Pagination

## Contents

- [The Basics](#the-basics)
- [Using The Query Builder](#using-the-query-builder)
- [Appending To Pagination Links](#appending-to-pagination-links)
- [Creating Paginators Manually](#creating-paginators-manually)
- [Pagination Styling](#pagination-styling)

<a name="the-basics"></a>
## The Basics

Laravel's paginator was designed to reduce the clutter of implementing pagination.

<a name="using-the-query-builder"></a>
## Using The Query Builder

Let's walk through a complete example of paginating using the [Fluent Query Builder](/docs/database/fluent):

#### Pull the paginated results from the query:

	$orders = DB::table('orders')->paginate($per_page);

25 26 27 28
You can also pass an optional array of table columns to select in the query:

	$orders = DB::table('orders')->paginate($per_page, array('id', 'name', 'created_at'));

Taylor Otwell committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#### Display the results in a view:

	<?php foreach ($orders->results as $order): ?>
		<?php echo $order->id; ?>
	<?php endforeach; ?>

#### Generate the pagination links:

	<?php echo $orders->links(); ?>

The links method will create an intelligent, sliding list of page links that looks something like this:

	Previous 1 2 ... 24 25 26 27 28 29 30 ... 78 79 Next

The Paginator will automatically determine which page you're on and update the results and links accordingly.

It's also possible to generate "next" and "previous" links:

#### Generating simple "previous" and "next" links:

	<?php echo $orders->previous().' '.$orders->next(); ?>

*Further Reading:*

- *[Fluent Query Builder](/docs/database/fluent)*

<a name="appending-to-pagination-links"></a>
## Appending To Pagination Links

You may need to add more items to the pagination links' query strings, such as the column your are sorting by.

#### Appending to the query string of pagination links:

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

This will generate URLs that look something like this:

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

<a name="creating-paginators-manually"></a>
## Creating Paginators Manually

Sometimes you may need to create a Paginator instance manually, without using the query builder. Here's how:

#### Creating a Paginator instance manually:

	$orders = Paginator::make($orders, $total, $per_page);

<a name="pagination-styling"></a>
## Pagination Styling

All pagination link elements can be style using CSS classes. Here is an example of the HTML elements generated by the links method:

82 83
	<div class="pagination">
		<a href="foo" class="previous_page">Previous</a>
Taylor Otwell committed
84

85 86
		<a href="foo">1</a>
		<a href="foo">2</a>
Taylor Otwell committed
87

88
		<span class="dots">...</span>
Taylor Otwell committed
89

90 91
		<a href="foo">11</a>
		<a href="foo">12</a>
Taylor Otwell committed
92

93
		<span class="current">13</span>
Taylor Otwell committed
94

95 96
		<a href="foo">14</a>
		<a href="foo">15</a>
Taylor Otwell committed
97

98
		<span class="dots">...</span>
Taylor Otwell committed
99

100 101
		<a href="foo">25</a>
		<a href="foo">26</a>
Taylor Otwell committed
102

103 104
		<a href="foo" class="next_page">Next</a>
	</div>
Taylor Otwell committed
105 106 107

When you are on the first page of results, the "Previous" link will be disabled. Likewise, the "Next" link will be disabled when you are on the last page of results. The generated HTML will look like this:

108
	<span class="disabled prev_page">Previous</span>