How to find the average value of items in a Laravel Collection?

October 26, 2018

Use the avg() method. You can provide a value indicating which key to average (first example) or not

(2nd example).

<?php
$average = collect(
        [
                ['foo' => 10],
                ['foo' => 10],
                ['foo' => 20],
                ['foo' => 40]
        ]
        )->avg('foo');
// 20
$average = collect(
        [1, 1, 2, 4]
        )->avg();
// 2