How to Get Previous / Next Record in Laravel

In some situations, like while creating a previous and next navigation, you might want to get the ID of record next to your current record to create a Next button. And ID of your previous record to create the Previous button. You can easily perform this task by using the following code.

Quick Tip : Updating Column Types with Laravel Schema Builder

$user = User::find(5);

// Get ID of Previous User
$prevUser = User::where('id', '<', $user->id)->max('id');

// Get ID of Next User
$nextUser = User::where('id', '>', $user->id)->min('id');

Source : Stack Overflow


Leave a Comment.