Pagination in Laravel 8 explained with examples
Laravel is one of the easiest and efficient PHP frameworks. It follows MVC or model, view, controller design pattern. It provides many exciting features for web development. Pagination is one of them. Laravel’s paginator uses Query builder and Eloquent ORM that provides suitable easy-to-use pagination. In this tutorial, you are going to learn about how to use pagination in laravel.
Prerequisite :
To complete this tutorial all you need to have is a configured computer, a text editor, a web browser, a web server installed on your computers like Xampp or WampServer, composer(package manager), and a basic understanding of PHP and laravel.
In this tutorial, we will use Sublime Text 3 as a text editor, xampp server, and Google Chrome as a web browser.
Topics To Be Covered In This Tutorial :
- What is pagination?
- When to use pagination?
- Methods of pagination
- Laravel query builder
- Laravel eloquent ORM
- A simple project on laravel pagination
What is pagination?
Pagination is the process of dividing a document into several pages. But in that case, like you have a user interface of an application where you will display database results into multiple pages.
When to use pagination?
There can be thousands of data for a single page and the page will take too much time to load. That would not be ideal for an application. If those data are divided into multiple pages, it would be easier to navigate and give a better user experience.
Methods of pagination
To perform pagination in laravel, you just have to use the paginate () function instead of all () or get() functions as used in maximum time. Paginate method takes a number as a parameter. It defines how much data you want to show on one page or section.
paginate(4);
Here paginate(4) means, four data will be shown on a single page or section. You’ll understand better in the example that is given below.
Laravel query builder
Laravel’s data query builder supplies a fluent easy interface to create and run database queries. It uses the table method to begin a query.
Syntax:
$variableName= DB::table('database_table_name')->all();
The table name has to be written inside the brackets within quotes. After that, when we write functions like all() or get() it retrieves the values from the database.
Laravel eloquent ORM
Laravel provides Eloquent ORM(object-relational mapper), which makes it easier to interact with your database.
Syntax:
$variableName= Database_model_name::all();
Eloquent ORM works with the database objects. We better know them as Models. In Laravel, each database table has a similar “Model”. When we call a model, it gets all the data from those tables with similar names.
Prerequisite :
To complete this project you need to have a basic understanding of HTML, PHP, and laravel.
Here in this example, you can see that we have a basic laravel controller, and inside that there is a function called viewData(). Our database model name is “Employee” and our database table name is “employees”. In the first operation, we’re using query builder where we perform paginate method on the “employees” table. It returns all employee data from the database and paginates them.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;
use DB;
class HomeController extends Controller
{
public function viewData(){
$showEmployeeData= DB::table('employees')->paginate(4); //Query Builder
//Or you can use
$showEmployeeData= Employee::paginate(4); //Eloquent ORM
return view('Employee.view', compact('showEmployeeData'));
}
}
Then we just simply pass the data through the compact method to use in HTML code for showing. Follow the steps below:
HTML CODE:
<div class="container">
<h1>Laravel pagination</h1><hr>
<table class="table">
<thead>
<tr>
<th scope="col">Employee id</th>
<th scope="col">Employee name</th>
</tr>
</thead>
<tbody>
@foreach($showEmployeeData as $data)
<tr>
<td>{{$data->id}}</td>
<td>{{$data->name}}</td>
</tr>
@endforeach
</tbody>
</table>
<h5>Pagination:</h5>
{{ $showEmployeeData->links() }}
</div>
After fetching the data from the database, we are just performing a foreach loop statement to view the data in the interface. And the links() is a method on laravel paginator class. The paginator class is returned when we call paginate on a model. In this example, the variable name is $showEmployeeData. But you can use any variable name that is comfortable and meaningful for you and your project.
Output of Pagination in Laravel
You can see in the output that, there are four data displayed on each page with the laravel pagination. But you can add more or less data on a single page as you need through pagination.
Conclusion:
In this tutorial, we learned how to use pagination in laravel and also completed a small project. Though, there are several ways to paginate items. But this is the easiest way to use the paginate method. This is a basic example to do it. If you understand and practice it, you can make your user interface more user-friendly.