Home » How To Add Laravel Next Prev Pagination

How To Add Laravel Next Prev Pagination

Last updated on January 4, 2021 by

Friends, In this article, we will learn how you can add Laravel next and prev pagination by the simple example. We will also see how to add next and prev pagination into the single page of Laravel. Let’s just jump into it.

Table of Contents
1. Add Next Prev Pagination On Listing Page
2. Add Pagination In Single Page In Laravel

01 Add Next Prev Pagination On Listing Page

To display a next prev link pagination Laravel provides a simple method called simplePagination

simplePaginate(number_of_records);

Let’s assume that we have a simple post model. We want to add pagination to the listing of our blog page. We also want to display “10 posts” by default with pagination. Let’s achieve our goal.

Create Route:

At the very first step, add the following route into your routes/web.php file. After adding the route you can now access URL like http://localhost/blog or http://localhost:8000/blog

Route::get('blog', 'PostController@index');

Create Controller:

Now, create a PostController by running php artisan make:controller PostController command in your CLI tool.

Then into the index() method of PostController write code as below:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class PostController extends Controller
{
    /**
     * The attributes that are mass assignable.
     * next prev pagination in laravel by scratchcode.io
     * @var array
     */
    public function index()
    {
        $posts = Post::simplePaginate(10);

        return view('posts.index', compact('posts'));
    }
}

Create View:

Next, create a index.blade.php file into your resources/views/posts directory. In this file, you can write your HTML + Laravel code to display the actual presentation of your page or site.

<!DOCTYPE html>
<html>
   <head>
      <title>Laravel Next Previous Pagination - ScratchCode.io</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
   </head>
   <body>
      <div class="container">

         <h1>Laravel Next Previous Pagination - ScratchCode.io</h1>

         <table class="table table-bordered">
            <tr>
               <th>ID</th>
               <th>Title</th>
            </tr>

            @if(!empty($posts))
               @foreach($posts as $post)
               <tr>
                  <td>{{ $post->id }}</td>
                  <td>{{ $post->title }}</td>
               </tr>
               @endforeach
            @endif
         </table>

         @if(!empty($posts))
         <div class="paginationWrapper">
            {{ $posts->links() }}
         </div>
         @endif
      </div>
   </body>
</html>

Alternate Way

The following way is useful when you want to add next and prev button into any kind of HTML structure.

@if(isset($posts))
   @if($posts->currentPage() > 1)
      <a href="{{ $posts->previousPageUrl() }}">Previous</a>
   @endif

   @if($posts->hasMorePages())
      <a href="{{ $posts->nextPageUrl() }}">Next</a>
   @endif
@endif

02 Add Pagination In Single Page In Laravel

Laravel provides simplePaginate method to show pagination on the listing page. But what if I want to show pagination on a single page. Let’s take the same example as above.

Now we already have a Post Model and Post Controller in our example. Our posts table also has slug column. So now if we can get the next and prev slug of the current post then we can easily show the next and prev pagination into the single page of the blog post. Let’s crack it together.

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class PostController extends Controller
{
    /**
     * The attributes that are mass assignable.
     * next prev pagination in laravel by scratchcode.io
     * @var array
     */
   public function show($id) {
       
      $post = Post::find($id);

      $previous = Post::select('slug')->where('id', '<', $post->id)->orderBy('id', 'desc')->first();

      $next = Post::select('slug')->where('id', '>', $post->id)->first();

      return view('post.single', ['post' => $post,'next' => $next, 'prev' => $previous]);
   }
}

resources/views/posts/single.blade.php

<!DOCTYPE html>
<html>
   <head>
      <title>Laravel Next Previous Pagination - ScratchCode.io</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
   </head>
   <body>
      <div class="container">

         <h1>Laravel Next Previous Pagination - ScratchCode.io</h1>

         <table class="table table-bordered">
            <tr>
               <th>ID</th>
               <th>Title</th>
            </tr>

            @if(!empty($posts))
               @foreach($posts as $post)
               <tr>
                  <td>{{ $post->id }}</td>
                  <td>{{ $post->title }}</td>
               </tr>
               @endforeach
            @endif
         </table>

         @if(!empty($posts))
         <div class="paginationWrapper">
            <a href="{{ url() }}/blog/{{ $prev->slug }}">Previous</a>
            <a href="{{ url() }}/blog/{{ $next->slug }}">Next</a>
         </div>
         @endif
      </div>
   </body>
</html>

Additionally, read our guide:

  1. Best Way to Remove Public from URL in Laravel
  2. Run PHP Artisan Commands On Shared Hosting Servers
  3. How To Calculate Age From Birthdate
  4. Active Directory Using LDAP in PHP or Laravel
  5. Laravel: Increase Quantity If Product Already Exists In Cart
  6. How To Use The Laravel Soft Delete
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Difference Between Events and Observers In Laravel
  10. Session Not Working In Laravel
  11. Laravel 8: Target Class Controller Does Not Exist

That’s it for now. We hope this article helped you to learn how to add next prev pagination in Laravel

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance. 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment

*

code