How to get id of next record in laravel

You can get the id of the next record in laravel using the min() method. The code snippets will return the next id which is greater than the specified id in the table.

Answers 1
  • Get id of next record of user table in laravel

    --PATH routes\web.php
    Route::get('/get-next-user-id/{id}', function($id) {
        $nextUserId = App\Models\User::where('id', '>', $id)->min('id');
        dd($nextUserId);
    });
    
    0
     
    Output 
    2
    OR
    null

    This code snippet will return the id of the user whose autoincremented ID is greater than the current user id. Like if you pass the parameter value as 1 then it will return the next user id  (2 or 3 or 4) which is greater and exists if no greater value exists in the id column then it will return the null value.

  • Back to code snippet queries related laravel