How to count in laravel collection?

by mallory_cormier , in category: PHP Frameworks , 5 months ago

How to count in laravel collection?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , 5 months ago

@mallory_cormier 

To count the number of items in a Laravel collection, you can use the count() method. Here's an example:

1
2
3
4
5
$collection = collect([1, 2, 3, 4, 5]);

$count = $collection->count();

echo $count; // Output: 5


Alternatively, you can also use the count() helper function like this:

1
2
3
$count = count($collection);

echo $count; // Output: 5


Both of these methods will give you the count of the items in the collection.