There are two ways to see data one is in ascending order and the other is in descending order. But by default, we can see the data after fetching in ascending order. Now we need to see the data in descending order according to id or date. How can we do how to get the latest data in Laravel 10?

In this tutorial, I will show you multiple ways that how to get the latest record from the database in the Laravel 10 application. We will see laravel get latest record by date then laravel get latest 5 records then laravel get last inserted record and then Laravel 10 get latest record.

There is an inbuilt latest() function to fetch the latest record from the database in Laravel. Without that, we can use SQL "desc" keyword to fetch the latest records. Also, we can use raw SQL to fetch the latest database records in Laravel. I will show you all of the ways.

Now let's first see how to fetch the latest records using a latest() helper.

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function index()
    {
        return User::latest()->get();
    }
}

 

We can also use order by in Laravel 10 to fetch the latest records from the database in Laravel. We just have to pass the key like id or date that how we want to see the latest record. According to date or according to last inserted id.

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function index()
    {
      return User::orderBy('id', 'desc')->get();
    }
}

 

Laravel get latest 5 records

Now assume we want to see only the last 5 or maybe 10 records. What will be the query? Let's see the query of Laravel get the latest 5 records. In this case, we can use limit() like below:

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function index()
    {
        return User::latest()->limit(5)->get();
    }
}

 

Let's see the second way how do you get the latest record with query builder?

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function index()
    {
        return DB::table('users')->latest()->get();
        //use one of them
        return DB::table('users')->orderBy('id', 'desc')->get();
    }
}

 

Laravel get last inserted record

Now if you want to see the last inserted record then see the below query:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function index()
    {
        return User::latest()->first();
        return User::orderBy('id', 'desc')->first();

        return DB::table('users')->latest()->first();
        return DB::table('users')->orderBy('id', 'desc')->first();
    }
}

 

And the last way of using raw SQL query. Now we will see how to fetch the latest() database records using raw sql query in Laravel. See the below query:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function index()
    {
        return DB::select('select * from users order by id desc');
    }
}

 

Read also: 3 Ways To Fetch Single Row From Database In Laravel

 

Conclusion

I have tried to demonstrate the concept of fetching the latest() records in the Laravel application. In this tutorial, we have seen latest() function, limit() function to fetch specific number of records and first() function to get only single records. Now we can say that we know laravel get last inserted record and laravel get latest record by date or id columns.