Laravel Get Next and Previous Record & URL Tutorial

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

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 *