DEV Community

Cover image for How to Get Previous and Next Record in Laravel
Suresh Ramani
Suresh Ramani

Posted on • Originally published at techvblogs.com

How to Get Previous and Next Record in Laravel

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();
Enter fullscreen mode Exit fullscreen mode

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.

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();
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Thank you for reading this blog.

Top comments (0)