How to Show Previous and Next Post in Laravel 10 - Web Journey

How to Show Previous and Next Post in Laravel 10 - Web Journey

Today we will learn how to show previous and next post in laravel. I will share you step by step how to show next and previous post in laravel 10.

 

 

To show the next and previous posts in Laravel, you'll need to follow these steps:

 

 

Step-1: Database Setup

Ensure that you have a database table that stores your posts.

 

 

 

Step-2: Define Routes

Define routes in your Laravel routes file  located in routes/web.php to handle the display of the next and previous posts.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;


Route::get('/{id}',[PostController::class, 'post_details'])->name('post.show');

 

 

 

Step-3: Create Controller 

Create a controller to handle the logic for retrieving the next and previous posts. You can generate a new controller using Laravel artisan command.

php artisan make:controller PostController

 

 

 

Step-4: Implement Methods

In the newly created controller, add methods to retrieve the next and previous posts based on the current post's ID.

 

PostController.php

<?php

namespace App\Http\Controllers;

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

class PostController extends Controller
{
    public function post_details($id)
    {
        $post_details = Post::findOrFail($id);
        $next_post = Post::where('id', '>', $id)->min('id');
        $previous_post = Post::where('id', '<', $id)->max('id');
        return view('welcome',compact('post_details','next_post','previous_post'));
    }
}

 

 

 

Step-5: Create Blade View

Create a Blade view or update the welcome.blade.php file to display the post details along with links to the next and previous posts, if available. In this view, you can use conditional statements to check if the next and previous posts exist and display appropriate links.

 

welcome.blade.php


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>How to Show Next and Previous Post in Laravel 10 - Web Journey</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
<div class="container mt-5">
    <div class="row">
        <div class="offset-md-3 col-md-6">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">{{ $post_details->title }}</h5>
                    <p class="card-text">{!! Str::limit($post_details->description,300 )!!}</p>

                    <hr>

                    @if($previous_post)
                        <a href="{{ route('post.show',$previous_post) }}" class="card-link btn btn-primary">Previous</a>
                    @else
                        <a href="javascript:void(0)" class="card-link btn btn-primary">Previous</a>
                    @endif

                    @if($next_post)
                        <a href="{{ route('post.show',$next_post) }}" class="card-link btn btn-success" style="float:right">Next</a>
                    @else
                        <a href="javascript:void(0)" class="card-link btn btn-success" style="float:right">Next</a>
                    @endif

                </div>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

 

That's it! Now when you visit the route that displays the individual post, it will show the post's details along with links to the next and previous posts if they exist.

 

Now paste the url: http://127.0.0.1:8000/1 in the browser and hit enter to see the output.

 

Output:

previous and next post

 

Hope this will help you a lot. Thanks.

 

 

 

You May Also Like Bellow Posts:

 

 

 

Laravel 9 live search data in a table using ajax. 

 

How to send SMS in laravel 9 using Twilio SMS API-Webjourney

 

Laravel 9 pdf invoice generate and download with barryvdh dompdf

 

How to create multi language website by laravel 9

 

Laravel 10 multiple form validation on the same page-WebJourney

 

 Laravel 10 Breeze Authentication - WebJourney

 

Laravel 10 Ajax jQuery Crud with Pagination and Live Search

 

Laravel Naming Conventions Accepted by Laravel community

 

Laravel Shorter and More Readable Syntax - WebJourney

Tags

  • Share This: