Source: panjeh.medium.com

Reduce an array in Laravel php
For Laravel Collections there is a method reduce()for reducing the collection to a single value. The reduce method passes the result of each iteration into the subsequent iteration. 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. Note: This method does the same job as the method array_reduce in PHP does.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners