Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify a page for pagination - Laravel 4

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);
like image 933
wyred Avatar asked Jun 30 '13 02:06

wyred


People also ask

How can I get current page in pagination Laravel?

you can easy and simply Pagination current Button in laravel. we can add current link on pagination using simplePaginate() in laravel 6, laravel 7 and laravel 8 application. laravel provide new eloquent method simplePaginate() for adding simple pagination with only current button link.

How can I use pagination in Laravel?

Use Pagination in Laravel Implementing pagination in laravel is smooth, add the laravel paginate function in the getData() function inside the EmployeeController class. Remove all() and use paginate() and it takes a number as an argument, that number defines the number of results to be shown to the user.

What is Paginator in Laravel?

The pagination option specifies which view should be used to create pagination links. By default, Laravel includes two views. The pagination::slider view will show an intelligent "range" of links based on the current page, while the pagination::simple view will simply show "previous" and "next" buttons.


2 Answers

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=.

like image 100
Andrew Avatar answered Oct 04 '22 14:10

Andrew


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(...);
like image 35
Jason Lewis Avatar answered Oct 04 '22 14:10

Jason Lewis