简体   繁体   English

Laravel 一页多分页

[英]Laravel Multiple Pagination in one page

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 Paginator 对其进行了分页。

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.现在我的问题是,例如,当您将 go 添加到第 2 页时,它会添加?page=2,但这也会使第一个表 go 也转到第 2 页。

Is there anyway to get something like this?有没有办法得到这样的东西?

page_table1={number}&page_table2={number}

so you don't apply the page change on other tables.因此您不会在其他表上应用页面更改。

$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?published = 3

laravel/public/articles?unpublished=1 laravel / public / articles?unpublished = 1

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();

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. setPageName()方法未在文档中进行记录,但是它是与文档中指示的方法一起使用的公共方法,因此应该可以正常工作。 FOr reference, this is the declaration (l. 171-180 in vendor/laravel/framework/src/Illuminate/Pagination/Environment.php ): 供参考,这是声明(l。171-180在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. 现在考虑到您将在URL后面附加另一个查询字符串,因此您需要告诉分页进行考虑。 Use the appends() method: 使用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() without the current index used by the paginator, or you'll end up with a double value). 也就是说,告诉每个Presenter使用所有查询字符串(由Request::query()生成的数组, 但不使用分页器使用的当前索引,来构建url Request::query() ,否则将得到一个double值)。 array_except() is a Laravel built in array helper that returns the given array (1st parameter) purged of the passed index (2nd parameter). 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! 无论如何都要让我知道!

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()}}    

Laravel 5.7 Laravel 5.7

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

10 = Max items per page

['*'] = colums

paramName = pagination param name

Illuminate\\Database\\Eloquent\\Builder 照亮\\数据库\\口才\\生成器

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 $pageName for 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'

Use: 使用:

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

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

For Laravel 5.2 对于Laravel 5.2

The answer of anonym may be extended to be very handy in the views that have related models. 在具有相关模型的视图中, 匿名答案可以扩展为非常方便。 tested in : 测试

Suppose we have a view for the CustomerController show.blade.php and we pass the Customer model 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() !!}

This is late, but it works with laravel 7.x这已经晚了,但它适用于 laravel 7.x

$messages = Message::where('status', 0)
    ->paginate(10, ['*'], 'p')
    ->appends(Arr::except(Request::query(), 'p'));

In your view, example(inbox.blade.php)在你看来,例如(inbox.blade.php)

{{$messages->links()}}

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个实例。

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. 理想情况下,您希望通过表ID THEN将活动页号存储在会话中,而不必通过URL为所有表设置当前页,您的脚本只需要担心指令,而不必担心确保其携带现有的页码。也。

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
}

Working in Laravel 8, paginate takes 4 params:工作在 Laravel 8, paginate需要 4 个参数:

Snippet: ( API Docs , Github )片段:( API 文档Github

public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)

Modifying the third param $pageName for each model, and appending all query params except the $pageName for that paginator should work:为每个 model 修改第三个参数$pageName ,并附加除该分页器的$pageName之外的所有查询参数应该有效:

FirstModel::query()
    ->paginate(null, ['*'], 'first_model')
    ->appends(request()->except('first_model'));

SecondModel::query()
    ->paginate(null, ['*'], 'second_model')
    ->appends(request()->except('second_model'));

Note, using ->appends(...) here ensures that all of the pagination links for that paginator will keep the current query param values.请注意,在这里使用->appends(...)可确保该分页器的所有分页链接都将保留当前查询参数值。

For example, if you're on the url ?first_model=2&second_model=3 the links for the first_model paginator will look something like:例如,如果您在 url ?first_model=2&second_model=3上,则first_model分页器的链接将类似于:

  • Previous: ?first_model=1&second_model=3上一个: ?first_model=1&second_model=3
  • Next: ?first_model=3&second_model=3下一步: ?first_model=3&second_model=3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM