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

Created 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

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.