Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this tutorial, I will give you a simple example of laravel pagination, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application, Sometimes we need to use paginate data in controller or Blade file in laravel application.
In core PHP we write more line code for pagination. But Laravel framework provides the best paginate method which is very simple and easy to use.
Laravel paginator is integrated with the Query builder and Eloquent ORM and provides convenient, easy-to-use pagination of database records with zero configuration.
We all know pagination is a basic requirement of each and every laravel application, So lets start with a simple pagination example in laravel with scratch.
php artisan make:migration create_pagination_examples_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePaginationExamplesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pagination_examples', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('book_name');
$table->string('book_author');
$table->longText('book_description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('pagination_examples');
}
}
php artisan:migrate
php artisan make:model PaginationExample
App\PaginationExample
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PaginationExample extends Model
{
protected $table = 'pagination_examples';
}
Now we insert some dummy records in our pagination_examples table, If you don’t know how faker and tinker work please read the given below article.
To enter the Tinker environment, run the tinker
Artisan command,
php artisan tinker
>>> Factory('App\PaginationExample',40)->create()
Now we have Sufficient records to paginate our pagination_examples table data in the Controller or Blade file.
Route::get('/list-data','ProductController@ListPaginationData')->name('list.data');
public function ListPaginationData(){
$paginateData = PaginationExample::paginate(10);
return view('paginationexample',compact('paginateData'));
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Laravel Pagination Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h3>Data</h3>
<table class="table">
<thead>
<tr>
<th>S.no</th>
<th>Book Name</th>
<th>Book Author</th>
</tr>
</thead>
<tbody>
@foreach($paginateData as $key => $data)
<tr>
<td>{{ $key+1 }}</td>
<td>{{$data->book_name}}</td>
<td>{{$data->book_author}}</td>
</tr>
</tbody>
@endforeach
</table>
<div class="mt-5" style="float:right">
{{ $paginateData->links()}}
</div>
</div>
</body>
</html>
Finally, we paginated the App\PaginationExample model and indicate that we plan to display 10 records per page. As you can see below the given images.
$users = DB::table('pagination_examples')->simplePaginate(15);
In this article, we learned “How to use pagination in Laravel”, I hope this article will help you with your Laravel application Project.