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.

#Signature

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;
2 
3// Create a new collection instance.
4$collection = new Collection([
5 'first', 'second', 'third'
6]);
7 
8// 3
9$collection->count();

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'
4]);
5 
6// 3
7$count = count($collection);

Some absolutely amazing
people

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.