For manual paging of laravel, laravel Paging

Source: Internet
Author: User
Tags tld

For manual paging of laravel, laravel Paging

Generally, we only need to useThe paginate method is easy to handle. But what about the combination of arrays? At this time, we need to use custom pages. First, let's take a look at laravel's paging method source code:

# Vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder. php: 480 public function paginate ($ perPage = null, $ columns = ['*'], $ pageName = 'page', $ page = null) {$ query = $ this-> toBase (); $ total = $ query-> getCountForPagination (); $ this-> forPage ($ page = $ page? : Paginator: resolveCurrentPage ($ pageName), $ perPage = $ perPage? : $ This-> model-> getPerPage (); return new LengthAwarePaginator ($ this-> get ($ columns), $ total, $ perPage, $ page, ['path' => Paginator: resolveCurrentPath (), 'pagename' => $ pageName,]);}
We found that the key is to use lengthAwarePaginator.
The construction method of LengthAwarePaginator is as follows:
public function __construct($items, $total, $perPage, $currentPage = null, array $options = []){        foreach ($options as $key => $value) {            $this->{$key} = $value;        }        $this->total = $total;        $this->perPage = $perPage;        $this->lastPage = (int) ceil($total / $perPage);        $this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path;        $this->currentPage = $this->setCurrentPage($currentPage, $this->lastPage);        $this->items = $items instanceof Collection ? $items : Collection::make($items);}

Assume that the array obtained by pagination is:
[  {    "id": 9,    "sys_id": 1,    "org_id": 2,    "user_id": 8,    "papaer_id": 26,  },  {    "id": 5,    "sys_id": 1,    "org_id": 2,    "user_id": 8,    "papaer_id": 26,  },  {    "id": 1,    "sys_id": 1,    "org_id": 2,    "user_id": 8,    "papaer_id": 26,  }]
Here there are three data entries in total. We set each page to display one and a total of three pages.
Paging call
Use Illuminate \ Pagination \ LengthAwarePaginator;
Use Illuminate \ Pagination \ Paginator;

Public function show (Request $ request ){
$ PerPage = 1; // The number displayed on each page
If ($ request-> has ('page ')){
$ Current_page = $ request-> input ('page ');
$ Current_page = $ current_page <= 0? 1: $ current_page;
} Else {
$ Current_page = 1;
}
$ Item = array_slice ($ real, ($ current_page-1) * $ perPage, $ perPage); // comment 1
$ Total = count ($ real );

$ Paginator = new LengthAwarePaginator ($ item, $ total, $ perPage, $ current_page ,[
'Path' => Paginator: resolveCurrentPath (), // comment 2
'Pagename' => 'page ',
]);
Return response ()-> json (['result' => $ paginator])
}

The focus of the above Code is $ item. If you do not perform annotation 1, all seven pieces of data are obtained.

Note 2 sets a url to be paged. You can also manually set it through $ paginator-> setPath ('path.

Page connections are also called in the same way {{$ paginator-> render ()}}

Note: The returned data format is roughly as follows:

"result": {    "current_page": 3,    "data": [
    {
      "id": 5,
      "sys_id": 1,
      "org_id": 2,
      "user_id": 8,
"papaer_id": 26
     }
], "from": null, "last_page": 2, "next_page_url": null, "path": "http://www.text.tld/examination/show", "per_page": 2, "prev_page_url": "http://www.text.tld/examination/show?page=2", "to": null, "total": 3 }


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.