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

2022-08-30 15:23:02

已经为此工作了太长时间,没有结果。我试过了。

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

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

\Paginator::setCurrentPage($current_page);

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

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

返回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);

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

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

我本来希望它像这样简单$model->find( )->paginate($per_page, $page)


答案 1

生成器类具有:

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

您可以致电

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

答案 2

假设您必须在 中分页,您可能会这样做:$usersUserController

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

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


推荐