Laravel 5 Collections: Getting the Number of Elements in a Collection With count

April 22, 2018 —John Koster

The count method will return the number of elements contained within the collection's inner array; it is synonymous with PHP's count function.


1public function count();

#Example Use

The count method is a straightforward method and simply returns the total number of items in the collection. The count method returns an integer.

1use Illuminate\Support\Collection;
3// Create a new collection instance.
4$collection = new Collection([
5 'first', 'second', 'third'
8// 3

It should also be noted that this method exists to implement PHP's Countable interface. This means that PHP's count function can be invoked on a collection instance:

1// Create a new collection instance.
2$collection = collect([
3 'first', 'second', 'third'
6// 3
7$count = count($collection);

Some absolutely amazing

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.