Laravel Eloquent - equivalent to first() for last?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Laravel Eloquent: The Equivalent of `last()` for Finding the Latest Record
As a senior developer working with Laravel and Eloquent, we constantly push the boundaries of what these tools can do. We love the expressive power of Eloquent relationships, but sometimes, when dealing with complex data interactions—especially finding the *latest* or *most recent* item—we run into limitations. A common question arises: Is there a direct `last()` method in Eloquent for navigating relationships?
The short answer is no, there isn't a dedicated `last()` method specifically for reversing the order of a relationship query. However, this limitation doesn't mean we can’t achieve our goal. We simply need to leverage Eloquent’s powerful ability to combine relationships with standard SQL ordering and limiting clauses.
Let’s dive into why this is the case and how we correctly find the latest records in our models.
## The Philosophy Behind `first()` vs. `last()`
Eloquent methods like `first()`, `where()`, and `with()` are designed to map directly to standard database operations. When you call `->first()`, Eloquent translates this into a `LIMIT 1` query (implicitly or explicitly) combined with whatever ordering you provide.
The concept of "first" is arbitrary; it depends entirely on the order defined in the `ORDER BY` clause. If you want the *latest* item, you must explicitly tell the database which record is considered the most recent.
Since there is no inherent semantic difference between "first" and "last," Eloquent relies on explicit instruction from the developer to determine the desired outcome. This makes the process robust but requires a conscious decision about sorting direction.
## Finding the Latest Record: The Correct Eloquent Approach
To find the "latest" record in a relationship (e.g., the most recently created post), you simply need to order by the primary key in descending order and then retrieve the first result. This is the standard, efficient way to handle this in any SQL environment.
Consider your `User` model and its `posts` relationship:
```php
// In the User Model
public function posts()
{
return $this->hasMany('Post');
}
```
To fetch the single most recent post for a user, you would perform the following query:
```php
$user = App\Models\User::find(1);
// Find the latest post associated with this user
$latestPost = $user->posts()
->orderBy('posts.id', 'desc') // Order by ID descending (newest first)
->first(); // Get only the top result
```
Notice how we explicitly use `orderBy('posts.id', 'desc')`. This tells Eloquent to instruct the database to sort all posts for that user by their primary key from largest to smallest, ensuring the post with the highest ID (which is typically the newest entry) comes first. This pattern is fundamental to efficient data retrieval in Laravel and adheres to best practices showcased by the team at [laravelcompany.com](https://laravelcompany.com).
## Handling Complex Relationships: `hasManyThrough` Sorting
Your example involving a `hasManyThrough` relationship adds another layer of complexity, especially when determining the most recent comment on a post.
If you have:
* `User` -> `posts` (many)
* `posts` -> `comments` (many)
And you want the *latest comment* for a specific post, you would apply the sorting logic directly to the relationship chain:
```php
// Assuming $post is an instance of the Post model
$latestComment = $post->comments()
->orderBy('comments.created_at', 'desc') // Order by the creation timestamp
->first();
```
In this scenario, sorting by `created_at` (a timestamp) is semantically superior to sorting by an arbitrary ID, as it reliably reflects the chronological order of events. By combining ordering and limiting, you effectively recreate the functionality of a theoretical `last()` method in a highly optimized manner.
## Conclusion
While Eloquent doesn't provide a single magical `last()` function for relationships, understanding how to combine `orderBy()` with relationship methods like `first()` is crucial. It shifts the responsibility from relying on an abstract function to controlling the underlying SQL execution. By mastering this pattern—ordering by the relevant timestamp or primary key in descending order before calling `first()`—you gain precise, efficient, and highly readable control over your data retrieval, making your Laravel applications significantly more powerful.