Published on April 22, 2018
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);
∎
John Koster
Learning text parsing, manipulation, encoding and more through the Laravel string helpers.
Published on December 7, 2016
Published on April 22, 2018
Published on April 15, 2018