【问题标题】:Laravel 5 multiple pagination in a single pageLaravel 5 单页多分页
【发布时间】:2017-01-29 02:31:08
【问题描述】:

我有一个包含两个表的页面,显示来自不同数据库的不同数据。我可以为这两个表进行分页,但是当我单击表 1 的第 2 页时,表 2 也将重定向到第 2 页。如何使表 2 停留在第 1 页,而当表 1 转到第 2 页时点击第 2 页。

【问题讨论】:

    标签: laravel-5 pagination


    【解决方案1】:

    如果你在 laravel 核心中看到,那么你会看到类似这样的分页功能

    /**
     * Paginate the given query.
     *
     * @param  int  $perPage
     * @param  array  $columns
     * @param  string  $pageName
     * @param  int|null  $page
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     *
     * @throws \InvalidArgumentException
     */
    public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
    {
        $total = $this->query->getCountForPagination();
        $this->query->forPage(
            $page = $page ?: Paginator::resolveCurrentPage($pageName),
            $perPage = $perPage ?: $this->model->getPerPage()
        );
        return new LengthAwarePaginator($this->get($columns), $total, $perPage, $page, [
            'path' => Paginator::resolveCurrentPath(),
            'pageName' => $pageName,
        ]);
    }
    

    现在在上面的函数中,你可以传递 $pagename 来使两个分页分别工作,如下所示

    $collectionA = ModelA::paginate(4, ['*'], 'pagination_a');
    $collectionB = ModelB::paginate(4, ['*'], 'pagination_b');
    

    或者您也可以为此使用 setPageName 函数

    【讨论】:

    • 非常感谢,感谢您的帮助,它现在正在工作。你拯救了我的一天。
    猜你喜欢
    • 2015-05-17
    • 2015-11-16
    • 2017-11-30
    • 2016-06-13
    • 1970-01-01
    • 2015-06-07
    • 2014-07-27
    • 2016-11-17
    • 2015-09-05
    相关资源
    最近更新 更多