Laravel custom page effect, laravel custom page

Source: Internet
Author: User

Laravel custom page effect, laravel custom page

For laravel paging, it comes with a paginate method, which is very useful but also has limitations.

Therefore, I wrote my own page for this. The specific code is as follows:

<? Php namespace ...; use ...; /*** custom paging class, suitable for queries with less data, * Class CustomPaginate * @ package App \ Tools \ Paginate */class CustomPaginate {/*** custom array pagination * @ param $ data = returned result *@ param $ page * @ param $ limit * @ return mixed */public static function paginate ($ data, $ page = 1, $ limit = 10) {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 Explanation Method for external exposure * @ param $ data = array | collection only supports these two types * @ param $ page = Current page * @ param $ limit = each several * @ return array | false * returned results are arrays * Call instances: customPaginate: paginateToArray ($ data, $ request-> page, $ request-> lim It); */public static function paginateToArray ($ data, $ page = 1, $ limit = 10) {$ isValidate = self: validate ($ data, $ page, $ limit); // verify if ($ isValidate = false) {return false;} $ res = self: paginate ($ data, $ page, $ limit) -> toArray (); // convert the paging data to an array // Previous Page | 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 parameter is valid */public static function validate (& $ data, & $ page = 1, & $ limit = 10) {$ page = empty ($ page )? 1: $ page; $ limit = empty ($ limit )? 10: $ limit; if (! Is_array ($ data )&&! $ Data instanceof Collection) {return false; // "the custom paging method only supports array data and set data";} if (! Is_numeric ($ page) |! Is_numeric ($ limit) {return false; // "the page limit parameter only supports numbers";} if ($ data instanceof Collection) {return $ data = $ data-> toArray () ;}return $ data ;}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.