Skip to content

Instantly share code, notes, and snippets.

@ermand
Created April 25, 2013 07:02
Show Gist options
  • Save ermand/5458012 to your computer and use it in GitHub Desktop.
Save ermand/5458012 to your computer and use it in GitHub Desktop.
Get Previus Next Record in Laravel
<?php
// Get the current user that will be the origin of our operations
$currentUser = User::find(10);
// Get ID of a User whose autoincremented ID is less than the current user, but because some entries might have been deleted we need to get the max available ID of all entries whose ID is less than current user's
$previousUserID = User::where('id', '<', $currentUser->id)->max('id');
// Same for the next user's id as previous user's but in the other direction
$nextUserID = User::where('id', '>', $currentUser->id)->min('id');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment