Laravel 5.1 указание текущей страницы для разбиения на страницы


Слишком долго работал над этим, но безрезультатно. Я пытался.

`\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)

Author: Shane, 2015-07-31

2 answers

Предположим, что у вас есть $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 и выше. Нужно это проверить.

 27
Author: amith.gotamey, 2015-08-06 05:11:54

Класс Конструктор имеет:

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

Вы можете позвонить

Model::find(...)->paginate($per_page, ['*'], 'page', $page);
 35
Author: PATROMO, 2016-09-30 12:57:55