【问题标题】:Laravel 5.1 specifing current page for paginationLaravel 5.1 指定当前页面进行分页
【发布时间】:2015-10-23 05:29:53
【问题描述】:

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

`\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) 一样简单

【问题讨论】:

  • 我也有这个问题,如果我解决了会告诉你

标签: php pagination laravel-5.1


【解决方案1】:

Builder 类有:

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

你可以打电话

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

【讨论】:

【解决方案2】:

假设你有$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'));
}

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

【讨论】:

  • 我必须使用\Illuminate\Pagination\Paginator::currentPageResolver 运行它,否则它会在当前名称空间中搜索类。使用 `\Paginator' 它告诉我找不到该类。这样就可以了!
  • Shane,你可以在控制器顶部有 imported 类:use Illuminate\Pagination\Paginator;
  • 完美。谢谢。
  • 你的代码应该添加一个 if 条件来检查用户是否要去其他页面,否则它会卡在同一页面上。
  • 虽然这可能有效,但我投了反对票,因为这只是简单地传递方法签名中已经存在的参数。
【解决方案3】:

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

getProducts?page=3

【讨论】:

  • 这真的取决于你的路由结构以及你如何使用框架。分页器默认使用 "first_page_url": "/?page=1" 但如果您不使用此结构,则它将不起作用。
猜你喜欢
  • 2019-10-24
  • 2013-06-27
  • 2016-01-23
  • 2016-01-05
  • 2018-05-01
  • 2021-07-03
  • 2016-03-16
  • 2010-10-19
  • 1970-01-01
相关资源
最近更新 更多