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.

Answers 2
  • 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 }}
    
    0
  • 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;
    });
    
    0
  • Back to code snippet queries related laravel