沃梦达 / 编程问答 / php问题 / 正文

一页中的 Laravel 多分页

Laravel Multiple Pagination in one page(一页中的 Laravel 多分页)

本文介绍了一页中的 Laravel 多分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的分页出了点问题.我有两个包含来自数据库的数据的表,我使用 laravel 分页器对它进行了分页.

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 中的 l. 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();

也就是说,告诉每个 Presenter 用所有查询字符串(由 Request::query() 产生的数组没有使用的当前索引来构建 url分页器,否则你最终会得到一个双倍值).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 多分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:一页中的 Laravel 多分页

基础教程推荐