php Laravel 5.1 指定当前页面进行分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31747801/
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 22:35:34  来源:igfitidea点击:

Laravel 5.1 specifing current page for pagination

phppaginationlaravel-5.1

提问by Shane

Been working on this for far too long with no results. I have tried.

在这方面工作了太久没有结果。我试过了。

`\Illuminate\Pagination\Paginator::setCurrentPage($current_page);`

returns Call to protected method Illuminate\Pagination\Paginator::setCurrentPage()

返回 Call to protected method Illuminate\Pagination\Paginator::setCurrentPage()

\Paginator::setCurrentPage($current_page);

\Paginator::setCurrentPage($current_page);

returns Call to protected method Illuminate\Pagination\Paginator::setCurrentPage()

返回 Call to protected method Illuminate\Pagination\Paginator::setCurrentPage()

\DB::getPaginator()->setCurrentPage($current_page);

\DB::getPaginator()->setCurrentPage($current_page);

returns call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'getPaginator'

返回 call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'getPaginator'

$tmp = new Post( ); $tmp->getConnection()->setCurrentPage($current_page);

$tmp = new Post( ); $tmp->getConnection()->setCurrentPage($current_page);

returns call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'getPaginator'

返回 call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'getPaginator'

How can I specify the page? I need to specify it manually.

如何指定页面?我需要手动指定它。

I had hoped it to be as easy as $model->find( )->paginate($per_page, $page)

我曾希望它像 $model->find( )->paginate($per_page, $page)

回答by PATROMO

The BuilderClass has:

生成器类有:

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

You can call

你可以打电话

Model::find(...)->paginate($per_page, ['*'], 'page', $page);

回答by amith.gotamey

Suppose you have $usersto paginate in your UserController, you might do:

假设你必须$users在你的 中分页UserController,你可能会这样做:

public function index()
{
    $currentPage = 3; // You can set this to any page you want to paginate to

    // Make sure that you call the static method currentPageResolver()
    // before querying users
    Paginator::currentPageResolver(function () use ($currentPage) {
        return $currentPage;
    });

    $users = \App\User::paginate(5);

    return view('user.index', compact('users'));
}

I believe this applies to Laravel 5.0 and above. Have to check on that.

我相信这适用于 Laravel 5.0 及更高版本。必须检查一下。

回答by Ahmad.Net

for those people who using api and they want to specify the current page in api, they can use extra parameter like this:

对于那些使用 api 并且他们想在 api 中指定当前页面的人,他们可以使用这样的额外参数:

getProducts?page=3

getProducts?page=3