Laravel Pagination

Sharad Jaiswal
Written by Sharad Jaiswal

Posted On: Sep 01, 2018

Laravel Pagination

 

Pagination in Laravel

According to Wikipedia Pagination is a process of separating or dividing a document or digital contents into discrete pages. In Core PHP and other frameworks, the paginating record is a painful task. Laravel Pagination provides convenient and easy-to-use database records pagination with the help of Laravel's paginator which is integrated by default with query builder and Eloquent ORM.

Here in this article, we are going to see how easy to implement pagination in Laravel.
While using Laravel you will find several methods to paginate records. Paginate method is the simplest and easiest way to implement Laravel Paginations. It will automatically configure the pagination's limit and offset based on the current page user is browsing.
Here is sample Example to implement pagination in Laravel 5.6 using DB facade and Eloquent way both.

Pagination using DB facade.

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
/**
* Show all of the users for the application.
*
* @return Response
*/
public function index()
{
$posts= DB::table('posts')->paginate(10);

return view('post.index', ['posts' => $posts]);
}
}

Pagination Code Explanation

In above example, we have created a sample Post controller and used DB facade to paginate our posts table data to 10 records.

Looking for Job Change?. Read some Laravel Interview Questions and Answers

Pagination using Eloquent Paginate method.

<php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Models\Post;
class PostController extends Controller
{
/**
* Show all of the users for the application.
*
* @return Response
*/
public function index()
{

$posts= Post::paginate(10);

return view('post.index', ['posts' => $posts]);
}
}

Code Explanation

In above example, we have created a sample Post controller and used Eloquent to paginate our posts table data to 10 records.

Displaying Results and paginations links in our View

<div class="container">
@foreach ($posts as $post)
{{ $post->title}}
@endforeach
</div>

{{ $posts->links() }}

Hope you understood how to implement pagination in Laravel.

Laravel Pagination

Pagination in Laravel 5.6Pagination in Laravel Framework

    1 Response

  •  Sharad Jaiswal
    Posted by oliverusselldev

    The eloquent method for using Laravel paginate is really easy. We just need to install the package, setup controller and routes and convert pagination results to json.

Please Login or Register to leave a response.

Related Articles

Laravel Tutorials

Laravel 5.5 Middleware complete Tutorial

In Laravel, you can think middleware as a system or tool that are used to filter all HTTP requests entering your application. Middleware works between request and response of our application. It sits ..

Laravel Tutorials

Laravel Pagination Ajax

Creating Ajax based pagination is Laravel 5.6. Ajax (Asynchronous JavaScript and XML) is a technique to update a specific part of webpage asynchronously without reloading the page. In this article, we..

Laravel Tutorials

Configuring Errors & Logging in Laravel 5

Laravel comes with excellent error and exception handling feature. When you install Laravel framework (MVC) error and exception handling is configured at the same time. In this tutorial, we will see h..

Ask a Question