How to get the id of last record from collection object in laravel view

Updated at 21-Aug-2021, By samar

How to get the id of last record from collection object in laravel view

In this article, we will see how to solve "How to get the id of last record from collection object in laravel view".

You can get the id of the last record from the collection object using the last() method in your view file. You can call the last() method with no arguments to get the last element in the collection.
  • Get id of last record from collection object in laravel view

    //routes\web.php
    Route::get('/get-id-of-last-record', function(){
        $posts = App\Models\Post::get();
        return view('index')->with(compact('posts'));
    });
    
    //resources\views\index.blade.php
    {{ $posts->last()->id }}
    
  • Get id of last record from collection object in laravel

    //routes\web.php
    Route::get('/get-id-of-last-record', function(){
        $posts = App\Models\Post::get();
        return $posts->last()->id;
    });
    

Back to code snippet queries related laravel