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 :
- Default index.php Root Path Change And Remove Public From The Url
- Laravel 9 File manager integration tutorial
- Laravel Event Broadcast With Redis Socket.io Echo To Listen Real-Time Message
- Laravel Logs Viewer Integration
- Laravel Carbon diffForHumans Arguments Passing
- map() Method | Laravel Collection Example
- How To Change The Views Or Blade File Path In Laravel
- Force Redirect to www From Non-www Using htaccess In Laravel
- All Know About Different Root Path Of The Laravel Application With Example
- How To Use Where Clause With Two Fields Of The Same Table Examples