Laravel - Pagination - Manually Creating A Paginator
Sometimes you may wish to create a pagination instance manually, passing it an array of items that you already have in memory. You may do so by creating either an Illuminate\Pagination\Paginator
, Illuminate\Pagination\LengthAwarePaginator
or Illuminate\Pagination\CursorPaginator
instance, depending on your needs.
The Paginator
and CursorPaginator
classes do not need to know the total number of items in the result set; however, because of this, these classes do not have methods for retrieving the index of the last page. The LengthAwarePaginator
accepts almost the same arguments as the Paginator
; however, it requires a count of the total number of items in the result set.
In other words, the Paginator
corresponds to the simplePaginate
method on the query builder, the CursorPaginator
corresponds to the cursorPaginate
method, and the LengthAwarePaginator
corresponds to the paginate
method.
When manually creating a paginator instance, you should manually "slice" the array of results you pass to the paginator. If you're unsure how to do this, check out the array_slice PHP function.