php 一页中的 Laravel 多分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24086269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:08:47  来源:igfitidea点击:

Laravel Multiple Pagination in one page

phplaravelpagination

提问by Melvin Koopmans

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.

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

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.

现在我的问题是,例如,当您转到第 2 页时,它会添加 ?page=2 但这会使第一个表格也转到第 2 页。

Is there anyway to get something like this ?

有没有办法得到这样的东西?

page_table1={number}&page_table2={number}

page_table1={number}&page_table2={number}

so you don't apply the page change on other tables.

所以你不要在其他表上应用页面更改。

采纳答案by Damien Pirsy

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:

不幸的是,我现在无法测试此代码,但是浏览文档和代码(在 中Illuminate/Pagination/Environment)我想您可能会像这样:

# use default 'page' for this
$collection1 = Model::paginate(20);

# use custom 'other_page' for this
$collection2 = Model2::paginate(20);
$collection2->setPageName('other_page');

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):

setPageName()方法未记录在文档中,但它是与文档中指示的方法一起使用的公共方法,因此它应该可以正常工作。供参考,这是声明(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;
}

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:

现在考虑到您将有另一个查询字符串附加到 url,因此您需要告诉分页考虑它。使用appends()方法:

$collection1->appends(array_except(Request::query(), 'page'))->links();

$collection2->appends(array_except(Request::query(), 'other_page'))->links();

That is, tell each Presenter to build up the url with all the query strings (the array resulting from Request::query()withoutthe 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).

也就是说,告诉每个 Presenter 使用所有查询字符串构建 url(由于Request::query()没有分页器使用的当前索引产生的数组,否则最终会得到一个 double 值)。array_except()是一个 Laravel 内置的数组助手,它返回清除了传递索引(第二个参数)的给定数组(第一个参数)。

I'll try to test this code as soon as I can, but it should work. Let me know in any case!

我会尽快尝试测试此代码,但它应该可以工作。无论如何请让我知道!

回答by anonym

$publishedArticles = Article::paginate(10, ['*'], 'published');
$unpublishedArticles = Article::paginate(10, ['*'], 'unpublished');

The third argument is used as follows:

第三个参数的用法如下:

laravel/public/articles?published=3

laravel/public/articles?unpublished=1

laravel/public/articles?published=3

laravel/public/articles?unpublished=1

回答by daVe

This is an easy solution I've found on Laravel 4.

这是我在 Laravel 4 上找到的一个简单的解决方案。

Controller

控制器

Change the page name before you make the paginator:

在制作分页器之前更改页面名称:

Paginator::setPageName('page_a');
$collection_A = ModelA::paginate(10);

Paginator::setPageName('page_b');
$collection_B = ModelB::paginate(10);

View

看法

Do the same: change the page name before you print the links

执行相同操作:在打印链接之前更改页面名称

Paginator::setPageName('page_a');
$collection_A->links();

Paginator::setPageName('page_b');
$collection_B->links();

If you don't want to lose any page state while you navigate to another page, append to the links the current page from all collections:

如果您不想在导航到另一个页面时丢失任何页面状态,请将所有集合中的当前页面附加到链接:

Paginator::setPageName('page_a');
$collection_A->appends('page_b', Input::get('page_b',1))->links();

Paginator::setPageName('page_b');
$collection_B->appends('page_a', Input::get('page_a',1))->links();

回答by mosleim

in laravel 5.3 (maibe working in other laravel 5 version), i use like this:

在laravel 5.3(maibe在其他laravel 5版本中工作)中,我是这样使用的:

    $produk = Produk::paginate(5, ['*'], 'produk');
    $region = Region::paginate(5, ['*'], 'region');

and in blade templating append links currents of other to keep the position:

并在刀片模板中附加其他链接电流以保持位置:

    {{$produk->appends(['region' => $region->currentPage()])->links()}}    
    {{$region->appends(['produk' => $produk->currentPage()])->links()}}    

回答by Rubens

Laravel 5.7

Laravel 5.7

Model->paginate(10, ['*'], 'paramName');

10 = Max items per page

['*'] = colums

paramName = pagination param name

Illuminate\Database\Eloquent\Builder

Illuminate\Database\Eloquent\Builder

回答by Jeremy Postlethwaite

In Laravel 5.2, declare the page name when using paginate().

在 Laravel 5.2 中,使用paginate().

Here is an example that works with multiple paginators on a page.

这是一个在页面上使用多个分页器的示例。

  • Be sure to specify a different $pageNamefor other models.
  • 一定要$pageName为其他型号指定一个不同的。

See the method \Illuminate\Database\Eloquent\Builder::paginate()

看方法 \Illuminate\Database\Eloquent\Builder::paginate()

/**
 * Get things by ownerId
 *
 * @param integer $ownerId The owner ID.
 *
 * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator Returns a pagination instance.
 */
public function getThings($ownerId)
{
    $builder = \App\Models\Things::where('ownerId', '=', (integer) abs($ownerId));

    // dd([
    //     '__METHOD__' => __METHOD__,
    //     '__FILE__' => __FILE__,
    //     '__LINE__' => __LINE__,
    //     '$ownerId' => $ownerId,
    //     'sql' => $builder->toSql(),
    //     '$builder' => $builder,
    //     'paginate' => $builder->paginate($perPage = null, $columns = ['*'], $pageName = 'p', $page = null),
    // ]);

    return $builder->paginate($perPage = null, $columns = ['*'], $pageName = 'p', $page = null);
}

Note: $pageName = 'p'

笔记: $pageName = 'p'

回答by LauroRafael

Use:

用:

$var1 = DB1::orderBy('...')->paginate(5, ['*'], '1pagination');

$var2 = DB2::orderBy('...')->paginate(5, ['*'], '2pagination');

For Laravel 5.2

对于 Laravel 5.2

回答by SaidbakR

The answer of anonymmay be extended to be very handy in the views that have related models. tested in laravel-5.4:

anonym的答案可能会扩展到在具有相关模型的视图中非常方便。laravel-5.4 中测试

Suppose we have a view for the CustomerController show.blade.phpand we pass the Customermodel to it using:

假设我们有一个 CustomerController 的视图show.blade.php,我们Customer使用以下命令将模型传递给它:

```

``

return view(['customer' => \App\Customer::find($customerId)]);

``` Each Customer has many Product, and has many Location and we want list with pagination both his/her Products and Locations, simply we may do it in the view like:

``` 每个客户都有很多产品,并且有很多位置,我们想要列出他/她的产品和位置的分页,我们可以在视图中这样做:

@foreach($customer->products()->paginate(10,['*'],'productsPage') as $product)
HTML list
@endforeach
{!! $customer->products()->paginate(10,['*'],'productsPage')->links() !!}

@foreach($customer->locations()->paginate(10,['*'],'locationsPage') as $location)
HTML list
@endforeach
{!! $customer->locations()->paginate(10,['*'],'locationsPage')->links() !!}

回答by Adam

Sounds to me like you need to update the paging tool so that it has an extra param that identifies the table as it's not smart enough to know that it might have 2 instances of itself on the same page.. DOH!

在我看来,您需要更新分页工具,以便它有一个额外的参数来标识表,因为它不够聪明,无法知道它可能在同一页面上有 2 个自身实例.. DOH!

Rather than having to set the current page for all tables through a URL, ideally you want to store the active page number in the session by table id THEN, your script only has to worry about instructions and not worry about ensuring it carries existing page numbers also.

不必通过 URL 为所有表格设置当前页面,理想情况下,您希望通过表格 ID 在会话中存储活动页码 THEN,您的脚本只需担心指令,而不必担心确保它携带现有页码还。

Here's a really draft concept....

这是一个非常草拟的概念......

// The page would be $tableId + "," + $pageNum
// Passed as a value of "page"
$goTo = "$tableId,$pageNum";
?page=$goTo

Then, when it gets to the part that's getting your table data it would do something like

然后,当它到达获取表数据的部分时,它会做类似的事情

if(!empty($_GET["page"])){
    list($tableId,$pageNum) = explode(",",$_GET["page"]);

    // Store table's active page number
    $_SESSION["_tableBrowser"][$tableId] = $pageNum;


    // Your table reader would then look at the session for the active page number
    // and not what is in $_GET
}