Laravel 5.1 paging implementation and custom paging style examples

Source: Internet
Author: User

Laravel also provides multiple implementation methods for paging. It can implement paging based on the query builder or the Eloquent model. The generated paging view is also compatible with the Bootstrap CSS style. At this point, it is estimated that many people are eager to try it out and want to wait to see how Laravel achieves paging. Let's say one by one:

1. Use the query builder to implement paging

Simple paging

We can call the simplePaginate method on the query builder to implement a style similar to the default page of WordPress (simple page link such as the previous page and next page ):

Class PostController extends Controller
{
/**
* Displays the document list.
     *
* @ Return Response
*/
Public function index ()
    {
// Use the query builder for simple paging. Three records are displayed on each page.
$ Posts = DB: table ('posts')-> simplePaginate (3 );
Return view ('post. Index', ['posts' => $ posts]);
    }

...
}

It is so simple that you do not need to pass in the page number or the total number, but simply call the simplePaginate method and pass in the number of entries displayed on each page. If you want to specify the queried field and page number parameter, the complete simplePaginate definition is as follows:

SimplePaginate ($ perPage = null, $ columns = ['*'], $ pageName = 'page ')

$ PerPage indicates the number of pages displayed, $ columns indicates the query field, and $ pageName indicates the page number name. The default page number name is page.

To view the page display effect, you also need to define the corresponding view file.

Pagination Blade view

Next, create the view file resources/views/post/index. blade. php on the document list page:

<Div class = "container">
<Ul>
@ Foreach ($ posts as $ post)
<Li >{{$ post-> title }}</li>
@ Endforeach
</Ul>
</Div>

{!! $ Posts-> render ()!!}

After saving the new file, go to the browser to access the http://laravel.app: 8000/post, the page is displayed as follows:

Click "& raquo;" next page to jump to http://laravel.app: 8000/post /? Page = 2 and the following content is displayed:

Of course, the paging style we usually see is more like this:

How can this be achieved?

Don't worry, Laravel has prepared corresponding implementation methods for us.

Implement pagination with page numbers

To implement the paging style with page numbers, you only need to call the paginate method on the query builder:

$ Posts = DB: table ('posts')-> paginate (3 );

Access the http://laravel.app again: 8000/post, the page displays as follows:

Obviously, the page already contains the page number. Of course, Bootstrap is not introduced here, so the style is ugly. However, the college will let everyone say goodbye to this style in the blog project launched next month, so I have to endure it for the time being.

Similarly, we can pass more parameters to the paginate method. The Complete parameter definition of paginate is as follows:

Paginate ($ perPage = null, $ columns = ['*'], $ pageName = 'page', $ page = null)

$ PerPage indicates the number of displayed pages, $ columns indicates the query field, $ pageName indicates the page number, and $ page indicates the page number.

2. Use the Eloquent model to implement paging

In addition to the query builder, Laravel also supports paging on the Eloquent model. Of course, we have mentioned in the previous Eloquent tutorials. In fact, the Eloquent model is essentially a query builder, therefore, in the Eloquent model, paging actually calls the methods on the builder.

Therefore, to implement simple paging, we can perform the following operations:

$ Posts = Post: simplePaginate (3 );

To implement pagination with page numbers, the call code is as follows:

$ Posts = Post: paginate (3 );

Of course, we can add more query conditions in the Eloquent model:

$ Posts = Post: where ('Views ','> ', 0)-> paginate (3 );

3. Custom paging implementation

By viewing the underlying implementation of simplePaginate and paginate functions, we can find that the two functions return Illuminate \ Pagination \ Paginator and Illuminate \ Pagination \ LengthAwarePaginator respectively. Therefore, if you want to implement the custom paging function, you need to input custom parameters to these two classes. Of course, these two classes can satisfy most of our needs, and more changes may be made in paging styles, for example, we want to modify the display of the link, and add a link like "homepage" and "last page" to the paging link. The pagination link style provided by Laravel is composed
The render Method of Illuminate \ Pagination \ BootstrapThreePresenter is generated. To customize the paging style, you need to post on these classes and methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.