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();
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// 39$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// 37$count = count($collection);
∎
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.