Skip to content

Pagination

Desmond edited this page Nov 24, 2015 · 4 revisions

Read the docs before proceeding!

Routes that have implemented pagination function:

  • /admin/*
  • /textbook/search

Paginations for admin pages are very easy, checkout Controllers/Admin/ProductController and views/admin/product/index.blade.php.

TextbookController method search():

$books = Book::searchByQuery($query);

// Get current page form url e.g. &page=1
if (Input::has('page'))
{
    $currentPage = LengthAwarePaginator::resolveCurrentPage() - 1;
}
else
{
    $currentPage = 0;
}

// Define how many items we want to be visible in each page
$perPage = config('pagination.limit.textbook');

// Slice the collection to get the items to display in current page
$currentPageSearchResults = $books->slice(($currentPage) * $perPage, $perPage)->all();

// Create our paginator and pass it to the view
$paginatedSearchResults= new LengthAwarePaginator($currentPageSearchResults, count($books), $perPage);

// Set paginator uri
$paginatedSearchResults->setPath('');

return view('textbook.list')
    ->with('books', $paginatedSearchResults)
    ->with('query', $query);

You should specify the pagination limit for your page in config/pagination.php.

Finally, add the following code to your view to display the pagination links:

{!! $books->appends(Request::only('query'))->render() !!}

Notice that we need to append the query input to the pagination link. Normally, we can just do {!! $books->render() !!} if there is no input involved.

Clone this wiki locally