Tuhin Hossain Tuhin Hossain - 2 years ago 64
HTML Question

group same name from array and show total of an indivisual group

I have an array which look like this:

Array
(
[0] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 4
[products_thickness_measurement] => 18
[product_ordered_pcs] => 4
[product_ordered_quantity] => 100
[others_feature] =>
[rate] => 500
[amount] => 50000
)

[1] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 4
[products_thickness_measurement] => 18
[product_ordered_pcs] => 0
[product_ordered_quantity] => 45
[others_feature] =>
[rate] => 45
[amount] => 2025
)

[2] => stdClass Object
(
[products_name] => Parla to gold(Flower)
[measurement] => 18
[unit] => mm
[products_size_height] => 0
[products_size_width] => 0
[products_size_unit] => inch
[products_type] => 2
[products_thickness_measurement] => 18
[product_ordered_pcs] => 4
[product_ordered_quantity] => 100
[others_feature] =>
[rate] => 850
[amount] => 85000
)
.......


Now I want to group the same product (product will be identified by combination of products_name, measurement and products_type) to show one product at a time and make total of "product_ordered_quantity" of the same product. See the image you may understand what I am trying to say. I will generate PDF file so no javascript please.

result:

enter image description here

I want:

enter image description here

Answer Source

If this is a query from the DB you can use some methods provided by the Collection class (https://laravel.com/docs/5.4/eloquent-collections)

$query; //The query object BEFORE getting the data, so before get() or paginate()
$data = $query->selectRaw('sum(product_ordered_quantity) as quantity')
              ->groupBy('products_name','measurement', 'products_type')
              ->get();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download