我正在使用Laravel version 5.2
。由于它的数据量很大,因此我正在使用光标pagiations
,如下表所示,该表具有primary id
和name
字段
for page 1 -> select * from table LIMIT 10
For page 2 -> select * from table where id < $lastpagelastementID LIMIT 10
以同样的方式,如果我们按name
列进行排序,我们如何处理此cursor pagination
?
可以选择吗?
我们可以按照
中的说明使用Laravel分页但是我需要一个以上的游标分页。谁能帮我找到解决方法?
答案 0 :(得分:0)
您可以使用以下方法通过composer安装此软件包:
composer require juampi92/cursor-pagination
配置
要将配置文件发布到config/cursor_pagination.php
,请运行:
php artisan vendor:publish --provider="Juampi92\CursorPagination\CursorPaginationServiceProvider" --tag="config"
工作方式
游标分页背后的主要思想是它需要一个上下文才能知道接下来要显示什么结果。因此,您说的是page=2
,而不是说next_cursor=10
。结果与老式页面分页相同,但是现在您可以对输出进行更多控制,因为next_cursor=10
应该始终返回相同的输出(除非删除某些记录)。
专业人士
缺点
分页查询生成器结果
有几种分页项目的方法。最简单的方法是使用查询构建器上的cursorPaginate
方法或口才查询。 cursorPaginate
方法自动负责设置适当的限制,并根据用户正在查看的光标获取下一个或上一个元素。默认情况下,通过HTTP请求上的页面查询字符串参数的值来检测cursor
。软件包会在考虑您的自定义配置的情况下自动检测到该值,并且还会自动将其插入到分页程序生成的链接和元数据中。
public function index()
{
$users = DB::table('users')->cursorPaginate();
return $users;
}
答案 1 :(得分:0)
我认为这可以解决您的问题。这是自定义分页
在Blade视图文件中:
<!-- pass variable page with a value
this generation of your pagination
should be based to the number of rows
of your table in the database and not
static. use foreach for this.
-->
<a href="pagination?page=1">1</a>
...
在您的控制器中:
public function pagination(Request $request){
// set the target page to 0 if page is 1 or null
// else get the value
$targetPage = ($request->input('page') == 1 || !$request->input('page')) ? 0 : ($request->input('page') - 1) ;
$noOfDataToGet = 10;
// get the number of data and skip the data based of target page * number of data
$tables = Tables::take($noOfDataToGet)->skip($targetPage * $noOfDataToGet)->get();
}
希望这会有所帮助