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.