How to get id of next record in laravel

Created at 16-Aug-2021 , By samar

How to get id of next record in laravel

We will use programming in this lesson to attempt to solve the "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.
  • 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);
    });
    
     
    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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.