doucang8303 2019-01-08 08:48
浏览 136
已采纳

带有列的PHP中的游标分页

I am using Laravel version 5.2. As its having large amount of data, I am using cursor pagiations as below table having primary id and name fields

for page 1 -> select * from table LIMIT 10

For page 2 -> select * from table where id < $lastpagelastementID LIMIT 10

As the same way, if we sort by the name column, How can we handle this cursor pagination?
Is there any option to do this?

we can use Laravel pagination as Explained in

Laravel Pagination

But i need a Cursor Pagination as above. Can any one help me to find solutions?

  • 写回答

2条回答 默认 最新

  • doupeizheng3918 2019-01-08 09:06
    关注

    You can install this package via composer using:

    composer require juampi92/cursor-pagination
    

    Config

    To publish the config file to config/cursor_pagination.php run:

    php artisan vendor:publish --provider="Juampi92\CursorPagination\CursorPaginationServiceProvider" --tag="config"
    

    How does it work

    The main idea behind a cursor pagination is that it needs a context to know what results to show next. So instead of saying page=2, you say next_cursor=10. The result is the same as the old fashioned page pagination, but now you have more control on the output, cause next_cursor=10 should always return the same (unless some records are deleted).

    Pros

    • New rows don't affect the result, so no duplicated results when paginating.
    • Filtering by an indexed cursor is way faster that using database offset.
    • Using previous cursor to avoid duplicating the first elements.

    Cons

    No previous page, although the browser still has them. No navigating to arbitrary pages (you must know the previous result to know the next ones).

    Paginating Query Builder Results

    There are several ways to paginate items. The simplest is by using the cursorPaginate method on the query builder or an Eloquent query. The cursorPaginate method automatically takes care of setting the proper limit and fetching the next or previous elements based on the cursor being viewed by the user. By default, the cursor is detected by the value of the page query string argument on the HTTP request. This value is automatically detected by the package taking your custom config into account, and is also automatically inserted into links and meta generated by the paginator.

    public function index()
    {
        $users = DB::table('users')->cursorPaginate();
        return $users;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?