How is Laravel custom paging implemented?

Source: Internet
Author: User
For Laravel paging, bring a paginate method, very useful, but there are limitations.

So I wrote a page of my own for this, the specific code is as follows

<?php namespace ...;   Use ...;/** * Custom paging class, suitable for small data queries, multi-data when not recommended * Class Custompaginate * @package app\tools\paginate */class custompaginate{/** * Custom number of Components page * @param $data = return result * @param $page * @param $limit * @return Mixed */public static function Pagi    Nate ($data, $page = 1, $limit = ten) {if (!is_numeric ($page) | |!is_numeric ($LIMIT)) {return false;    } $count = count ($data);    $data = Array_slice ($data, ($page-1) * $limit, $limit);  return new Lengthawarepaginator ($data, $count, $limit, $page);   }/** * Parameter interpretation method of external exposure * @param $data = array|collection Remember to support only these two * @param $page = Current Page * @param $limit = show a few of each page    * @return Array|false * Returns the result of an array of call instances: Custompaginate::p aginatetoarray ($data, $request->page, $request->limit); */public static function Paginatetoarray ($data, $page = 1, $limit = ten) {$isValidate = Self::validate ($data, $pa           GE, $limit);    Verify if ($isValidate = = = False) {return false; } $res = selF::p aginate ($data, $page, $limit)->toarray (); Paging data conversion to an array//previous | |        Next page = Path if ($res [' prev_page_url ']! = null) {$prev _page = $page-1; $res [' prev_page_url '] = Paginator::resolvecurrentpath (). "? page=". $prev _page. "&limit=".      $limit;        if ($res [' next_page_url ']! = null) {$next _page = $page + 1; $res [' next_page_url '] = Paginator::resolvecurrentpath (). "? page=". $next _page. "&limit=".      $limit;  } return $res; }/** * Verify that the parameters are valid */public static function validate (& $data, & $page = 1, & $limit = ten) {$page = empt Y ($page)?    1: $page; $limit = Empty ($limit)?    Ten: $limit;    if (!is_array ($data) &&! $data instanceof Collection) {return false;//"custom paging method only supports array data and collection data";    } if (!is_numeric ($page) | |!is_numeric ($LIMIT)) {return false;//"page limit parameter only supports numbers";    } if ($data instanceof Collection) {return $data = $data->toarray ();  } return $data;}} 

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.