Get the Latest Related Record in Laravel Using latestOfMany()

Stop manually sorting relationships when you only need the most recent record.

  • 10 Jun, 2026
  • 24 Views

Get the Latest Related Record in Laravel Using latestOfMany()

Many developers do this:

$latestOrder = $user->orders()
    ->latest()
    ->first();

It works, but you'll end up repeating this logic everywhere.


The Better Approach

Laravel provides:

latestOfMany()

Define the Relationship

public function latestOrder()
{
    return $this->hasOne(Order::class)
        ->latestOfMany();
}

Usage

$user->latestOrder

That's it.

No:

->latest()->first()

everywhere in your codebase.


Real Project Example

Imagine an e-commerce application.

Instead of:

$order = $user->orders()
    ->latest()
    ->first();

You can simply write:

$order = $user->latestOrder;

Cleaner and more expressive.


Eager Loading

$users = User::with('latestOrder')->get();

Now every user includes only their latest order.

Perfect for:

  • Dashboards
  • Admin panels
  • API responses


Share: