duanlianyun0462 2014-06-06 16:08
浏览 39
已采纳

Laravel Multiple Pagination在一页中

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.

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.

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.

  • 写回答

9条回答 默认 最新

  • dry9192 2014-06-06 19:39
    关注

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

    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;
    }
    

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

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料