一页中的Laravel多重分页 [英] Laravel Multiple Pagination in one page
问题描述
我的分页有些麻烦. 我有两个包含来自数据库的数据的表,并使用laravel Paginator对它进行了分页.
I'm having some trouble with my pagination. I'm having two tables with data from a database and I paginated it with laravel Paginator.
现在我的问题是,当您转到第2页时,它添加了?page = 2,但这也使第一个表也转到了第2页.
Now my problem is when you go to, for example, page 2 it adds ?page=2 but that makes the first table go to page 2 too.
反正有这样的东西吗?
page_table1={number}&page_table2={number}
因此您不会在其他表上应用页面更改.
so you don't apply the page change on other tables.
推荐答案
不幸的是,我现在无法测试此代码,但是浏览文档和代码(在Illuminate/Pagination/Environment
中),我想您可能是这样的:
Unfortunately I can't test this code right now, but browsing at the docs and the code (in Illuminate/Pagination/Environment
) I guess you could something like this:
# use default 'page' for this
$collection1 = Model::paginate(20);
# use custom 'other_page' for this
$collection2 = Model2::paginate(20);
$collection2->setPageName('other_page');
setPageName()
方法未在文档中记录,但与文档中指示的方法一起是公共方法,因此它应该可以正常工作.对于引用,这是声明(vendor/laravel/framework/src/Illuminate/Pagination/Environment.php
中的171-180):
The setPageName()
method isn't documented in the docs, but it's a public method alongside those indicated in the documentation, so it should be working fine. FOr reference, this is the declaration (l. 171-180 in vendor/laravel/framework/src/Illuminate/Pagination/Environment.php
):
/**
* Set the input page parameter name used by the paginator.
*
* @param string $pageName
* @return void
*/
public function setPageName($pageName)
{
$this->pageName = $pageName;
}
现在考虑到您将在URL后面附加另一个查询字符串,因此您需要告诉分页进行考虑.使用 appends()
方法:
Now take into consideration that you will have another query string appended to the url, so you need to tell the pagination to consider it. Use the appends()
method:
$collection1->appends(array_except(Request::query(), 'page'))->links();
$collection2->appends(array_except(Request::query(), 'other_page'))->links();
也就是说,告诉每个演示者使用所有查询字符串构建url(Request::query()
没有产生的数组,即分页器使用的当前索引,否则您将得到以下结果)双精度值). array_except()
是Laravel内置的数组助手,它返回给定的数组(第一个参数),该数组清除了传递的索引(第二个参数).
That is, tell each Presenter to build up the url with all the query strings (the array resulting from Request::query()
without the current index used by the paginator, or you'll end up with a double value). array_except()
is a Laravel built in array helper that returns the given array (1st parameter) purged of the passed index (2nd parameter).
我将尽快尝试测试此代码,但它应该可以工作.无论如何让我知道!
I'll try to test this code as soon as I can, but it should work. Let me know in any case!
这篇关于一页中的Laravel多重分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!