Reduce an array in Laravel php
Laravel PHP Reduce Array
For Laravel Collections there is a method reduce()
for reducing the collection to a single value.
Suppose we have a Laravel Collection

We can use simple php foreach loop to iterate through the items and access to their costs and calculate the total cost like:

Alternative ( the better way ):
The alternative in Laravel is to use the reduce
method.

The reduce
method passes the result of each iteration into the subsequent iteration.
Note: This method does the same job as the method array_reduce
in PHP does.