Specify a page for pagination - Laravel 4

2019-05-01 20:06发布

问题:

I'm trying to "remember" the page the user was on as he browses through records so that when he returns to the list, he is returned to the page where he left off.

How do I change the "current page" value for paginator?

I've tried Input::set('page', $x); but there's no such function. $_GET['page'] = $x; doesn't work too.

This the code:

$list = Publication::orderBy($this->data['sort_by'], $this->data['order_by']);

foreach ($this->data['filter_data'] AS $field => $value) {
    $list->where($field, 'LIKE', "%$value%");
}

$this->data['list'] = $list->paginate(15);

回答1:

You can adjust the page of the pagination environment through the DB connection.

DB::getPaginator()->setCurrentPage(2);

Not entirely sure but you might be able to go through your model with something like this.

Publication::getConnection()->setCurrentPage(2);

If the above isn't working (as it seems from your comment), then do it with an instance of Publication.

$publication = new Publication;

$publication->getConnection()->setCurrentPage(2);

$list = $publication->orderBy(...);


回答2:

I looked at the API --- turns out this is much easier now in 2014.

All you have to do is set

Paginator::setCurrentPage(2);

any time before you call ->paginate(), I believe, and it should override the page set (or not set) by ?page=.



回答3:

Try

$pager->setCurrentPage(2);