Laravel Get Next and Previous Record & URL Tutorial

To Get Previous Next Record and url in Laravel; In this tutorial, you will learn how to get the next or previous record or data with URL in Laravel application.

Sometimes, when you work with a blog application in laravel. So At that time you need to get the next record from the database table and the previous record from the database table.

How to Get Previous and Next Record, url in Laravel

Steps to get next and previous record or data, url from database table in laravel application.

1. Get previous record or data

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

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

This laravel eloquent query is users where() and orderBy() methods to fetch previous records like title, URL, slug, etc from the database table in laravel

2. Get Next record or data

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();

This Laravel query uses where(), first() and orderBy() to fetch next records from DB table.

Note: – To access data obtained from $next or $ previous variable. You can use it like this:

//id
 $previous->id
 //slug
 $previous->slug
 //id

 $next->id
 //slug
 $next->slug

Displaying the next and previous posts url. So you can show like this:

<div class="row">
    <div class="col-md-6">
        @if (isset($previous_record))
            <div class="alert alert-success">
            <a href="{{ url($previous_record->slug) }}">
                <div class="btn-content">
                    <div class="btn-content-title"><i class="fa fa-arrow-left"></i> Previous Post</div>
                    <p class="btn-content-subtitle">{{ $previous_record->title }}</p>
                </div>
            </a>
            </div>
        @endif
    </div>
    <div class="col-md-6">
        @if (isset($next_record))
        <div class="alert alert-success">
        <a href="{{ url($next_record->slug) }}">
            <div class="btn-content">
                <div class="btn-content-title">Next Post <i class="fa fa-arrow-right"></i></div>
                <p class="btn-content-subtitle">{{ $next_record->title }}</p>
            </div>
        </a>
        </div>
        @endif
    </div>
</div>

Conclusion

That’s it; you have learned how to get next previous url and record in laravel web apps.

Recommended Laravel Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

2 replies to Laravel Get Next and Previous Record & URL Tutorial

  1. Great Article.

    I’ve used this and it works.

    thanks lot

Leave a Reply

Your email address will not be published. Required fields are marked *