How to get the last item from a Laravel database query array
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Get the Last Item from a Laravel Database Query Array Efficiently
As developers working with Laravel, we frequently interact with database queries that return collections of data. A common requirement is to extract specific information from the end of this collection—for instance, finding the most recently created record or the last entry in an ordered list.
Let's walk through a practical scenario where you query a set of users and need to capture the ID of the very last user retrieved. We will explore several methods, ranging from simple PHP collection manipulation to optimized database querying techniques.
The Scenario Setup
Imagine you are fetching all users from your database using the standard Laravel Query Builder:
// routes.php or a Controller method
$users = DB::table('users')->get();
When iterating over this $users collection in your view, accessing the last item requires specific logic:
{{-- In users.blade.php --}}
<ul>
@foreach ($users as $user)
<li>{{ $user->id }}</li>
@endforeach
</ul>
{{-- Attempting to access the last ID directly (which would fail without prior calculation) --}}
<div id="last-user" data-user-id="{{ $users->lastUserId ?? 'N/A' }}"></div>
While it seems straightforward, Laravel Collections (which are based on PHP arrays) don't offer a built-in lastUserId property. We need to leverage native PHP collection methods or optimize the database query itself.
Method 1: Using Native PHP Collection Methods
The most direct way to achieve this is by utilizing PHP's built-in functions available on Laravel Collections. Since $users is a Illuminate\Support\Collection, we can use the last() method to retrieve the final element.
To get the last user object, you would access it like this:
$lastUser = $users->last();
if ($lastUser) {
$lastUserId = $lastUser->id;
} else {
$lastUserId = null; // Handle case where the collection is empty
}
This method is simple and effective for collections that are already loaded into memory. It requires a second step after the initial query, which is perfectly acceptable if you are only dealing with small result sets.
Method 2: Optimizing the Database Query (The Best Practice)
While Method 1 works, relying on fetching all records just to get the last one can be inefficient, especially with large tables. A more performant and robust approach is to let the database do the heavy lifting using orderBy and limit. This approach minimizes the data transferred over the network and significantly improves query execution time.
To reliably get the last item based on a sort order (e.g., by creation date), you should order your results in descending order (desc) and limit the result set to just one record.
Here is how you would rewrite the query:
// Querying for the most recently created user
$latestUser = DB::table('users')
->orderBy('created_at', 'desc') // Order by creation time descending
->limit(1) // Only retrieve the top 1 record
->first(); // Execute and fetch the single result as a model/object
if ($latestUser) {
$lastUserId = $latestUser->id;
} else {
$lastUserId = null;
}
This method is superior because it avoids loading thousands of unnecessary rows into your application memory. For complex queries, mastering these database-level optimizations is crucial when building robust applications with Laravel. As you explore more advanced Eloquent features and query optimization techniques, remember that understanding the relationship between the database and PHP objects is key to writing high-performance code.
Conclusion
To summarize, while you can extract the last item from an existing collection using $users->last(), the most developer-friendly and performant solution for retrieving the single last record is to leverage SQL capabilities directly within your query using orderBy('column', 'desc') and limit(1). This ensures that your application only processes the exact data required, leading to cleaner code and better performance. Always aim to push filtering and ordering logic down to the database whenever possible, a principle central to effective Laravel development.