Join WhatsApp ChannelJoin Now

Laravel 8 Get Next / Previous Record

Hi,

Today, i we will show you laravel 8 get next / previous record. This article will give you simple example of laravel 8 get next / previous record. you will learn laravel 8 get next / previous record. So let’s follow few step to create example of laravel 8 get next / previous record.

Get Previous Record

We have a table named posts, we want to take previous record with the help of Laravel database table. So you can use the below eloquent query for that.

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

Get Next Record

We have a table named posts, we want to take next record with the help of Laravel database table. So you can use the below eloquent query for that.

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

If you want to see the url of next and previous post then 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>

Recommended Posts