devianvisuals's avatar

Pagination onEachSide Method not working

Could someone explain to me how to use the database pagination onEachSide method?

Controller:

$paginate = DBTable::paginate(2);	// DBTable has 20 rows
return view(
    'adjusting-the-pagination-link-window',
    ['paginate' => $paginate]
);

View:

{{ $paginate->onEachSide(2)->links() }}
// Pagination Output: < 1 2 3 4 5 6 7 8 9 10 >

Variable Contents:

Illuminate\Pagination\LengthAwarePaginator {#2236 ▼ // app\Http\Controllers\AdjustingThePaginationLinkWindowController.php:14
  #items: Illuminate\Database\Eloquent\Collection {#1616 ▶}
  #perPage: 2
  #currentPage: 1
  #path: "http://127.0.0.1:8000/adjusting-the-pagination-link-window"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▶]
  #total: 20
  #lastPage: 10
}
0 likes
2 replies
Hzu's avatar
Hzu
Best Answer
Level 2

onEachSide() refers to the page numbers on both left and right side of the current page number. By default, the value is 3. Meaning, it will always return 3 links on both left and right sides of the current page number.

Example: Current page is 10 and onEachSide is set to 3.

< 1 2 ... 7 8 9 [10] 11 12 13 .. 29 30 >

Since you only have 10 pages, it will always give you the whole 10 links when onEachSide is set to 3 and above. Try setting it to 2 or 1 to see the effect immediately. Personally, I prefer 1 so it becomes less cluttered.

1 like

Please or to participate in this conversation.