How to Get Previous and Next Record in Laravel

Suresh Ramani - Author - TechvBlogs  Suresh Ramani   Laravel   1 month ago
How to Get Previous and Next Record in Laravel - TechvBlogs

A really common task is to display links for the previous and next record typically you find these on blogs, when viewing the post a previous title and link are displayed and the same for the next post, as long as they are previous and next posts. In this post, I'll explain how to create these.

There are several ways to implement this functionality and the simplest way is to use the Model Id. Since by right an Id auto increment, we can assume that the newest post will always have a higher number than the oldest post.

Laravel gets previous & next record, data, or URL examples. This tutorial will help us learn how to get the next/previous data or record in laravel application throughout this comprehensive tutorial.

How to Get Previous and Next Record in Laravel

Here are the quintessential steps you needed to follow to get the next or previous data and records from the database table in the laravel.

Get Next Record in Laravel

Get the next record or data from the database in the laravel app. We have one table name posts, we want to fetch the next record or data from the database table in laravel. So you can use the below eloquent query for that:

$next_record = Post::where('id', '>', $post->id)->orderBy('id')->first();

As you can see, we have defined the where()first()orderBy() likewise first() eloquent queries to get the next posts or records from the database.

Checkout Tool: Campaign URL Builder

Get Previous Record in Laravel

Define the $previous variable, along with the Blog model. Set the where() clause, pass the id and blog id properties and define the orderBy and first queries to fetch the previous records and post URL or slug from the database.

$previous_record = Blog::where('id', '<', $blog->id)->orderBy('id','desc')->first();

Access Prev / Next Records from Laravel Blade View

Open the blade view template; you can get the idea from the below code example to show the previous and next blog posts with their respective slug and URLs.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>How to Get Previous and Next Record in Laravel</title>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-6">
                @if (isset($previous_record))
                    <a href="{{ url($previous_record->url) }}">
                        <div> Previous</div>
                        <p>{{ $previous_record->title }}</p>
                    </a>
                @endif
            </div>
            <div class="col-6">
                @if (isset($next_record))
                    <a href="{{ url($next_record->url) }}">
                        <div>Next</div>
                        <p>{{ $next_record->title }}</p>
                    </a>
                @endif
            </div>
        </div>
    </div>
</body>
</html>

Thank you for reading this blog.

Read Also: How to change the URI (URL) for a remote Git repository?

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

Tags:
Laravel PHP

Suresh Ramani - Author - TechvBlogs
Suresh Ramani
Full Stack Developer