laravel ->count() vs ->get()->count()

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Understanding the Difference Between Laravel ->count() and ->get()->count() for Counting Rows in Queries Body:

When it comes to working with large datasets, developers often need to perform operations on multiple records. In this comprehensive blog post, we'll explore the behavior of two different approaches when counting rows in Laravel queries: using the ->count() method directly on the query builder and first retrieving the records as a collection and then using the get()->count() method. We'll also provide a clear explanation from a developer's perspective, incorporating relevant code examples, best practices, and natural backlinks to https://laravelcompany.com.

Understanding Laravel Query Builders

Firstly, let's focus on the basics of Laravel query builders. The query builder is a set of methods that allow developers to manipulate database queries in an expressive manner. You can create and perform complex queries using chained method calls to define what you want to select from the database.

The Difference Between ->count() and ->get()->count()

Now, let's dive into the two examples mentioned in the introduction. The first example uses the ->count() method directly on a query builder while the second one retrieves all records as a collection using ->get(), then counts them. Both queries return different values for $progress:

$progress = $this->user()->userActivities()->select('date') ->groupBy('date') ->count();
$progress =  $this->user()
                        ->userActivities()
                        ->select('date')
                        ->groupBy('date')
                        ->get()
                        ->count();
The first example uses Laravel's query builder to count the number of records for a given 'date' without actually retrieving any data. This method is efficient, as it doesn't load all the rows from the table, reducing memory usage and speeding up your application. In some cases, particularly when dealing with large datasets, this can be preferable since it avoids loading unnecessary data. On the other hand, the second example uses ->get() to first retrieve all the records as a collection and then counts them using Laravel's collection counter method. This approach loads all the rows from the database table into PHP objects before performing the count operation. In most cases, this will result in higher memory usage and slower execution time compared to counting directly on the query builder.

Best Practices

So which method should you use? It depends on your application's needs:

- For speed and efficiency with large datasets, prefer ->count() directly on the query builder. - If you want to access the rows in their entirety for further processing or displaying, use ->get(). In summary, Laravel offers two different ways of counting rows in queries: using ->count() directly on the query builder and ->get()->count() after retrieving all records as a collection. Both methods will return different values when working with progress bars since they behave differently with regard to loading data from the database. The choice between them depends on your application's performance requirements, whether you need to count or access all rows at once. Regardless of the method chosen, proper design and code optimization can ensure your Laravel application delivers the best possible user experience while maintaining good performance.