eloquent laravel: How to get a row count from a ->get()

eloquent laravel: How to get a row count from a ->get()

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Eloquence vs. Efficiency: Counting Rows After ->get() in Eloquent

When you execute a query using Eloquent's ->get() method, you are explicitly asking the database to retrieve all matching records and hydrate them into model instances. In your specific case, you ran \DB::table('wordlist')->where('id', '<=', $correctedComparisons)->get(). This returns an instance of Illuminate\Support\Collection containing every row that matches your filter.

The confusion often stems from mixing up collection methods with database query builders. You mentioned trying $wordlist->count() and count($wordlist). Both of these approaches are actually valid, but they operate at different layers of abstraction with distinct performance implications.

If you call $wordlist->count() on the collection returned by ->get(), Laravel leverages the underlying PHP array count. Since ->get() has already fetched all rows from the database into memory, this operation is instantaneous and incurs no additional database hits. It simply returns the number of items currently in that specific collection instance. This is perfectly fine if you are building a small utility or running a script where performance is not critical.

However, using count($wordlist) relies on PHP's native SplCountable interface. In almost every standard Laravel version and PHP environment, this yields the exact same result as $wordlist->count(). There is rarely a functional difference between the two when working with Eloquent collections; they both iterate over the underlying array to determine size.

The real issue arises if your goal is to avoid a second database request entirely while still getting an accurate count, or if you are concerned about memory usage on large datasets. In your scenario, you have already forced a full retrieval of data by calling ->get(). At that exact moment in the code lifecycle, you possess all the data required to determine the row count. You do not need a second database request because the first one (->get()) already satisfied the query plan. The "second request" fear usually applies when you want a count before fetching the data, or if you mistakenly think you are making two separate queries when you aren't.

If you needed to know the count without fetching the rows at all, you would use ->count() on the query builder instance before calling ->get(). This sends a single SQL statement (typically SELECT COUNT(*)) to the database, which is significantly more efficient than retrieving every row and then counting them in PHP.

$count = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)->count();
$wordlist = \DB::table('wordlist')->where('id', '<=', $correctedComparisons)->get();

In this pattern, the first line executes a COUNT(*) query. The second line executes a SELECT * query. While modern SQL optimizers are smart, sending two separate queries for simple pagination or listing logic is generally considered an anti-pattern because of the overhead.

Since you have already invoked ->get(), the data is in memory. Calling $wordlist->count() immediately after is the correct approach to inspect the size of that specific collection without triggering another SQL round-trip. If you attempt to call ->count() on the query builder after calling ->get(), Laravel will likely throw an error or behave unexpectedly because the builder has been consumed and converted into a model/collection instance. The object reference changes from a Query Builder to a Collection, so you must ensure you are calling methods appropriate for the current object type.

For further reading on optimizing database interactions in Laravel, including strategies for eager loading and query performance tuning, you might find the resources at https://laravelcompany.com useful. Understanding when to stop the query building process and switch to collection manipulation is a key skill in maintaining scalable applications.

Our network