In this post, you will learn how to average to price in the collection using the laravel average method. Laravel provides a method for averaging the collection of the amount given.
We can calculate the given price of the array for all of the items in the collection by simply passing the value in the param key or a simple array will make it average.
The average method is a useful method for calculating array prices all into an average sum.
In the first example simple average is given while the second example gives average with key param pass in example 3 take with tax average with price and example 4 take only active status make average only.
Example : average() | Collection Example
Here below are four working examples given to understand the average method how to use in the collection, also you can use in collection method in model eloquent.
// EXAMPLE 1 $data = [ 10, 20, 30 ]; $collection = collect($data)->average(); // OUTPUT EXAMPLE 1 // 20 // --------------------------------------------------- // EXAMPLE 2 $data = [ ['price' => 100], ['price' => 200], ['price' => 300], ]; $collection = collect($data)->average('price'); // OUTPUT EXAMPLE 2 // 200 // --------------------------------------------------- // EXAMPLE 3 $data = [ ['price' => 100, 'tax' => 50], ['price' => 200, 'tax' => 50], ['price' => 300, 'tax' => 50], ]; $collection = collect($data)->average(function($item){ return $item['price'] + $item['tax']; }); // OUTPUT EXAMPLE 3 // 250 // --------------------------------------------------- // EXAMPLE 4 $data = [ ['price' => 100, 'tax' => 50, 'status' => true], ['price' => 200, 'tax' => 50, 'status' => true], ['price' => 300, 'tax' => 50, 'status' => false] ]; $collection = collect($data)->average(function($item){ if($item['status']){ return null; } return $item['price'] + $item['tax']; }); // OUTPUT EXAMPLE 4 // 350
Above example will help you to make average calculate of amount in collection laravel.
We always thanks to you for reading our blogs.

Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Laravel Eloquent Parent Child In Same Table With Example
- Laravel Delete Image From Storage Folder
- Laravel 9 Create Custom Auth Guard Multi Login Authentication System Tutorial
- How To Generate Digital Invoice PDF In Laravel Demo
- Arr::accessible() | Laravel Helper Function
- Middleware Example With Authentication Laravel 9
- Arr::collapse() | Laravel Helper Function
- Livewire Create Multiple Select2 Dynamic Dropdown In Laravel 9 8 Example
- Different Solutions For Fix 419 Page Expired laravel
- Laravel 6 REST API with Passport Tutorial