How to get last record from object collection in laravel

Updated at 21-Aug-2021, By samar

How to get last record from object collection in laravel

We’ll attempt to use programming in this lesson to solve the "How to get last record from object collection in laravel" puzzle.

You can get the last record from object collection in laravel using the last() method. The last method helps you to get the last record from an object in laravel.
  • Get last record from object in laravel

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

    You can copy code and paste in your web.php file and run php artisan serve command and click below link to get the output

    http://localhost:8000/get-last-record

    Output : 

    {"id":5,"title":"Aliquid sit sit beatae.","description":"Modi veniam est dolores minus voluptatem consectetur ut culpa. 
    Est minus dolor ullam neque sapiente. Aut magni quidem molestiae exercitationem ut beatae. Adipisci id eligendi mollitia autem.",
    "created_at":"2021-08-02T06:08:40.000000Z","updated_at":"2021-08-02T06:08:40.000000Z","category_id":null,"status":"0","user_id":6}

Back to code snippet queries related laravel