Get last record from table in laravel

Created at 25-Aug-2021 , By samar

Get last record from table in laravel

We will use programming in this lesson to attempt to solve the "Get last record from table in laravel".

Sometimes we want to get the last record from the table. We can get the last record from the table in laravel using latest() and orderBy('id', 'DESC')->first() method. It will help you to get the last record on the basis of the highest value in the id column or the latest entry in the table on the basis of the created_at field.
  • Get last record from users table using latest()

    $lastUser = DB::table('users')->latest()->first();
    //Or get the last record on the basis of id field which helps you get the accurate result if created_at column value is null
    $lastUser = DB::table('users')->latest('id')->first();
    
    //Step by step implementation of code
    //routes\web.php
    use App\Http\Controllers\HomeController;
    Route::get('/get-last-record', [HomeController::class, 'getLastUser']);
    
    //app\Http\Controllers\HomeController.php
    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Models\User;
    use DB;
    class HomeController extends Controller
    {
        public function getLastUser(){
            $lastUser = DB::table('users')->latest()->first();
            print_r($lastUser);
        }
    }
    

    This code snippet will help you to get the last record of users table on the basis of created_at field if not argument passed to latest() method and will display the last record after using the first() method. Like if you have 3 records with id 1,2 and 3 then it will return the last record with id 3.

    If you pass the id field to the latest method then it will return the last record on the basis of the id field of the users table. It will return accurate data in case if create_at field value is null.

  • Get last record from users table using orderBy() with first() method

    $lastUser = DB::table('users')->orderBy('id', 'DESC')->first();
    
    //Step by step implementation of code
    //routes\web.php
    use App\Http\Controllers\HomeController;
    Route::get('/get-last-record', [HomeController::class, 'getLastUser']);
    
    //app\Http\Controllers\HomeController.php
    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Models\User;
    use DB;
    class HomeController extends Controller
    {
        public function getLastUser(){
            $lastUser = DB::table('users')->orderBy('id', 'DESC')->first();
            print_r($lastUser);
        }
    }
    

    It will get the last record of the users table by using orderBy() method on the id field. Like if you have 3 records with id 1, 2, and 3 then it will return the last record which has id 3.

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.