To calculate the average of values in a numeric collection using Illuminate/Collections, you can follow these steps:
use Illuminate\Support\Collection;
$collection = collect([1, 2, 3, 4, 5]);
avg()
method to calculate the average of the numeric values in the collection:$average = $collection->avg();
$average
variable to display or perform further operations with the calculated average.Here's a complete example:
use Illuminate\Support\Collection;
$collection = collect([1, 2, 3, 4, 5]);
$average = $collection->avg();
echo "Average: " . $average;
This will output:
Average: 3