Next Previous Link Button Pagination in Laravel

Websolutionstuff | Jun-14-2021 | Categories : Laravel

Today we will learn next previous link button pagination in laravel, Using paginate method you can easily create pagination in laravel.

Here i will show you laravel next and prev pagination with example, we can create customize next and previous pagination link in Laravel sso let's see how to create pagination in laravel 8.

If you only need to display simple "Next" and "Previous" links in your website , then you can use the simplePaginate method to perform a single, efficient query.

Example 1 : 

1) create controller and add below code.

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use App\Post;
 
class PostController extends Controller
{
    
    public function index()
    {
        $posts = Post::simplePaginate(10);
 
        return view('posts.index', compact('posts'));
    }
}

 

2) create blade file.

<html>
   <head>
      <title>Next Previous Link Button Pagination in Laravel - websolutionstuff.com</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
   </head>
   <body>
      <div class="container">
 
         <h1>Next Previous Link Button Pagination in Laravel - websolutionstuff.com</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>

 

Example 2 :

@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

 

 

Recommended Post
Featured Post
Laravel 9 MongoDB CRUD Operation Tutorial
Laravel 9 MongoDB CRUD Operati...

In this article, we will see the laravel 9 MongoDB crud operation tutorial. In laravel, we will have many crud operation...

Read More

Dec-06-2022

How To Integrate Razorpay Payment Gateway In Laravel 9
How To Integrate Razorpay Paym...

In this article, we see how to integrate razorpay payment gateway in laravel 9. As you all know if you are developi...

Read More

Apr-11-2022

Laravel 8 Form Class Not Found
Laravel 8 Form Class Not Found

In this small post, we will solve the laravel 8 form class not found error, many time we have received errors like the l...

Read More

Mar-12-2021

Laravel 10 Scout Search and Algolia Example
Laravel 10 Scout Search and Al...

Hey everyone! Ever wished your Laravel application could deliver lightning-fast search results? Well, I've got great...

Read More

Feb-21-2024