How to Get Previous and Next Record in Laravel?

Admin   Laravel   162  2021-03-14 11:40:05

Hi Guys,

In this example,I will learn you how to get previous and next record in can easy and simply get previous and next record in laravel.

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

Get Next Record in Laravel

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.


$next = User::where('id', '>', $user->id)->orderBy('id')->first();

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 = User::where('id', '