GENERATE PAGINATION IN LARAVEL LIKE A PRO DECODEWEB.IN
Paginations in Laravel is a way to fetch limited model data in chunks with functionality to represent fetched data in dynamic links as pages.
TABLE OF CONTENTS • What is pagination in Laravel ?
• Why do we need to paginate in Laravel ?
• How to implement Eloquent pagination using Laravel ?
• get() vs paginate()
• Generate html pagination links using Bootstrap css
Generate pagination in laravel like a pro
Source: ​https://decodeweb.in/php/php-frameworks/laravel-framework/generate-pagination-in-laravel-like-a-pro/
What is pagination in Laravel ? Paginations in Laravel is a way to fetch limited model data in chunks with functionality to represent fetched data in dynamic links as pages. That is, let's take an example of a book. A book a collection information represented as pages. A page contains a small portion of information which a book holds. In a similar way, when laravel interact with models, it fetches information from databases collectively and put on user’s webpage.
Why do we need to paginate in Laravel ? We are not required to paginate our data when we are damn sure that information from DB is very little, but can we guarantee it always ?, Of Course NOT. Let's assume a database query which takes 0.01 seconds to fetch 50 records from a table, and then pass this data into foreach loop to display in html table, let me ask you one simple question then, how much time will the same operation take for 10000 rows ? That’s a huge time as a developer perspective but as per user perspective, will a user scroll down to 10000 rows on a single webpage ? A definite No. That is why we need to paginate data to save time as well as to enhance the User eXperience.
How to implement Eloquent pagination using Laravel ? get() vs paginate() There are two main methods to get data from Eloquent ORM in laravel. get() method fetches all the records from a database table and it should only be used in background processing tasks. paginate() method accepts an integer parameter denoting a number of data to fetch from a collection. By default, even if we do not pass this parameter then it is set to 10 records per page.
Generate html pagination links using Bootstrap css UserController.php public function getUsers(Request $request) { $per_page = $request->per_page ?? 10; $users = App\User::fetchUsers($per_page); return view('users',compact($users)); } App\User.php Public static function fetchUsers($per_page) { return self::paginate($per_page); } users.blade.php
<table class="table table-hover"> <thead> <tr> <th scope="col" >#ID</th> <th scope="col" >Username</th> <th scope="col" >First Name</th> <th scope="col" >Last Name</th> </tr>
</thead> <tbody class="rw-shadow"> @foreach($users as $key => $user) <tr> <td > # {{ $key + $users->firstItem() }}</td> <td > {{ $user->username }}</td> <td > {{ $user->first_name }}</td> <td > {{ $user->last_name }}</td> </tr> @endforeach </tbody> </table> <!-- paginations -- > {{ $users->links('pagination') }} pagination.blade.php @if ($paginator->hasPages()) <div class="row text-right mr-2"> <nav aria-label="Page navigation" class="ml-auto"> <ul class="pagination"> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <li class="page-item"> <a class="page-link" href="#" aria-label="Previous"> <span aria-hidden="true">«</span> <span class="sr-only">Previous</span> </a> </li> @else <li class="page-item"> <a class="page-link" href="{{ $paginator->previousPageUrl() }}" aria-label="Previous"> <span aria-hidden="true">«</span> <span class="sr-only">Previous</span> </a> </li> @endif
{{-- Pagination Elements --}} @foreach ($elements as $element) {{-- "Three Dots" Separator --}} @if (is_string($element)) <li class="page-item disabled"><span>{{ $element }}</span></li> @endif {{-- Array Of Links --}} @if (is_array($element)) @foreach ($element as $page => $url) @if ($page == $paginator->currentPage()) <li class="page-item"><a class="page-link active" href="#">{{ $page }}</a></li> @else <li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li> @endif @endforeach @endif @endforeach
{{-- Next Page Link --}} @if ($paginator->hasMorePages()) <li class="page-item"> <a class="page-link" href="{{ $paginator->nextPageUrl() }}" aria-label="Next"> <span aria-hidden="true">»</span> <span class="sr-only">Next</span> </a> </li> @else <li class="page-item"> <a class="page-link" href="#" aria-label="Next"> <span aria-hidden="true">»</span> <span class="sr-only">Next</span> </a> </li> @endif
</ul> </nav> </div> @endif Output: